代码之家  ›  专栏  ›  技术社区  ›  Abhishek Agarwal

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

  •  2
  • Abhishek Agarwal  · 技术社区  · 8 年前

    我知道从当前窗口创建窗口对象时重新定位窗口的过程( 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();
    

    我有一个类似的代码 当前窗口 也但是我有一个后退按钮 targetwindow 隐藏了 目标窗口 show() 这个 当前窗口 。我使用信号槽机制实现了这一点,如下所示:

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

    问题是在按下“上一步”按钮后 目标窗口 这个 当前窗口 显示但不保持我之前为其设置的位置(即屏幕中间)。我试图通过使用此操作符添加用于在绘制事件中定位它的代码来更正它,但它不起作用:

    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()函数,以及如何重写?

    编辑 :正在添加我的项目的代码。此处当前窗口->主窗口和目标窗口->对话

    对话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
    

    主窗口。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
    

    主要的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();
    }
    

    主窗口。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();
    }
    

    对话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();
    }
    

    主窗口。用户界面

    <?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>
    

    对话用户界面

    <?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>
    

    如果进行“确定”->背面->在这个循环中,您会发现主窗口一直向上移动。

    1 回复  |  直到 8 年前
        1
  •  4
  •   Abhishek Agarwal    8 年前

    是否尝试了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();