Before the QT programming mostly only involves the simple multi-window, did not encroach on the value interaction between the window, wants or "inexperienced not deep", to the QT understanding, the application also needs diligently diligently.
This time the problem is this, I want to achieve a similar QQ, Alibaba chat client, after the pop-up dialog box, the ability to select the expression and return to the Chat window message input box, which requires two of the value of the interaction between the windows. The first solution that comes to mind is naturally the signal/slot mechanism using QT. That is, the first signal to the parent window, the parent window to the signal execution slot function sends a signal to carry the required data to the child window. But the question comes: the signal to receive the parent window in the child window must be signal and slot bound. This requires the definition of the main window class (1). Worried about the recursive inclusion of the header file, we had to think of another way. How about accessing the members of a parent window class directly in a child window using a pointer to the parent window? Just do it, write a test code and try it out. Mainwindow.ui has a lineedit and pushbutton, click to pop up the dialog instance, Dialog.ui, there is a pushbutton, click on the MainWindow to modify the text content of Lineedit. The code is as follows: (1) mainwindow.h
#ifndef Mainwindow_h #define Mainwindow_h #include <QMainWindow> Namespace Ui { Class MainWindow; } Class Mainwindow:public Qmainwindow { Q_object Public Explicit MainWindow (Qwidget *parent = 0); ~mainwindow (); void Setlinetext (QString str); Private Slots: void on_pushbutton_clicked (); Private Ui::mainwindow *ui; }; #endif//Mainwindow_h |
(2) Mainwindow.cpp
#include "Mainwindow.h" #include "Ui_mainwindow.h" #include "Dialog.h" #include "Ui_dialog.h" Mainwindow::mainwindow (Qwidget *parent): Qmainwindow (parent), UI (New Ui::mainwindow) { UI->SETUPUI (this); } Mainwindow::~mainwindow () { Delete UI; } void Mainwindow::on_pushbutton_clicked () { ' This ' is very important. Subwin.setgeometry (x () -100, Y () +100, wifth () -100, height ()-100); Needed. } void Mainwindow::setlinetext (QString str) { Ui->lineedit->settext (str); } |
(3) Dialog.h
#ifndef Dialog_h #define Dialog_h #include <QDialog> Namespace Ui { Class Dialog; } Class Dialog:public Qdialog { Q_object Public Explicit Dialog (Qwidget *parent = 0); ~dialog (); Private Slots: void on_pushbutton_clicked (); Private Ui::D ialog *ui; }; #endif//Dialog_h |
(4) Dialog.cpp
#include "Dialog.h" #include "ui_dialog.h" #include "mainwindow.h" #include "ui_mainwindow.h Dialog::D ialog (Qwidget *parent): Qdialog (parent), UI (new UI::D ialog) { ui->setupui (this); } dialog::~dialog () { delete ui; } void dialog::on_pushbutton_clicked () { MainWindow *ptr = ( mainwindow*) Parentwidget (); //" parentwidget " very important. Gets the parent part pointer, which requires a strong-type turn. Ptr->setlinetext ("I can do it, too."); close (); } |
http://blog.csdn.net/jan5_reyn/article/details/38955707
Qt Code: A child window calls the parent window (which is actually accessed directly by the pointer)