在qt中更改show()上的窗口位置



我知道从当前窗口(currWindow(创建其对象时重新位置窗口的过程:

QDesktopWidget widget;
QRect screenGeometry = widget.screenGeometry();
int height = screenGeometry.height();
int width = screenGeometry.width();
//set to middle of screen
targetwindow->move((width - targetwindow->width()) / 2.0,
                (height - targetwindow->height()) / 2.0);
targetwindow->show();

我也有类似的currWindow代码。但是,我在targetwindow中有一个返回按钮,它隐藏了targetwindowshow() currWindow。我已经使用信号插槽机制实现了这一点:

connect(targetwindow,&TargetWindow::pressTargetWindowGoBackButton,
                                           this,&CurrentWindow::show);

问题是在按下targetwindow上的后退按钮后,currWindow显示了我之前设置的位置(即屏幕中间(。我试图通过添加用于使用此操作员在油漆事件中定位的代码来对其进行纠正,但它不起作用:

void currWindow::paintEvent(QPaintEvent *pe)
{
        QDesktopWidget widget;
        QRect screenGeometry = widget.screenGeometry();
        int height = screenGeometry.height();
        int width = screenGeometry.width();
        this->move((width - this->width()) / 2.0,
                        (height - this->height()) / 2.0);
}

我该怎么做?我需要覆盖预定义的show((函数吗?

编辑:添加我项目的代码。这里currentwindow-> mainwindow和targetWindow->对话框。

dialog.h

#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
    Q_OBJECT
signals:
    void goBack();
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
private slots:
    void on_pushButton_clicked();
private:
    Ui::Dialog *ui;
};
#endif // DIALOG_H

mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void on_pushButton_clicked();
private:
    Ui::MainWindow *ui;
    Dialog *dia;
};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    QDesktopWidget widget;
    QRect screenGeometry = widget.screenGeometry();//.availableGeometry(widget.primaryScreen());
    int height = screenGeometry.height();
    int width = screenGeometry.width();
    w.move(-50000,-50000);
    w.move((width - w.width()) / 2.0,
                    (height - w.height()) / 2.0);
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    dia=new Dialog;
    connect(dia,&Dialog::goBack,this,&MainWindow::show);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    QDesktopWidget widget;
    QRect screenGeometry = widget.screenGeometry();//.availableGeometry(widget.primaryScreen());
    int height = screenGeometry.height();
    int width = screenGeometry.width();
    dia->move(-50000,-50000);
//    dia->show();
    dia->move((width - dia->width()) / 2.0,
                    (height - dia->height()) / 2.0);
    dia->show();
    hide();
}

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}
Dialog::~Dialog()
{
    delete ui;
}
void Dialog::on_pushButton_clicked()
{
    emit goBack();
    hide();
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>900</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>150</x>
      <y>90</y>
      <width>201</width>
      <height>141</height>
     </rect>
    </property>
    <property name="text">
     <string>OK</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>900</width>
     <height>19</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

dialog.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>900</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>370</x>
     <y>120</y>
     <width>231</width>
     <height>151</height>
    </rect>
   </property>
   <property name="text">
    <string>BACK</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

如果您继续使用OK-> back-> OK循环中的循环,您会发现主窗口保持向上移动。

您是否尝试过targetWindow的setGeometry((函数:

int height = screenGeometry.height();
int width = screenGeometry.width();
int x=(width - w.width()) / 2.0;
int y=(height - w.height()) / 2.0;
w.setGeometry(x,y,w.width(),w.height());
w.show();

相关内容

  • 没有找到相关文章