In the MainWindow defines a myPushButton pointer,
The class MainWindow: public QMainWindow { Q_OBJECT Public: MainWindow (QWidget * parent=nullptr); ~ MainWindow (); Void guanbi (); Private: Ui: : MainWindow * Ui; MyPushButton * nBtn;//& lt; -- -- -- }; This is the definition of myPushButton
The class myPushButton: public QPushButton { Q_OBJECT Public: Explicit myPushButton (QWidget * parent=nullptr); ~ myPushButton (); Void response (); Signals: Void mys ();//& lt; The launch of the signal -}; Void myPushButton: : response () { QDebug () & lt; & lt;" The response to myclicked!" ; } Then the same signal mys,
MainWindow: : MainWindow (QWidget * parent) : QMainWindow (parent) , the UI (new UI: : MainWindow) { The UI - & gt; SetupUi (this); NBtn=new myPushButton; NBtn - & gt; Move (100200); NBtn - & gt; The resize (50, 100); NBtn - & gt; NPushbutton setText (" "); NBtn - & gt; SetParent (this); SetWindowTitle (" test window "); SetFixedSize (800600); The connect (nBtn, & amp; MyPushButton: : mys, this, & amp; MainWindow: : close);//& lt; Function - associated signals and slots, Guanbi ();//& lt; - transmitting } MainWindow: : ~ MainWindow () { QDebug () & lt; & lt;" MainWindow 's destructor!" ; Delete the UI; } Void MainWindow: : guanbi () { QDebug () & lt; & lt;" Emit signal!" ; Emit nBtn - & gt; Mys (); } According to the print run, emission signal is normal, but the MainWindow cannot shut (namely MainWindow always displayed after running, not closed), if to replace the connect function with the following way, the response function can run normally, suggesting that connect the signal there is no problem, but why can't perform the close?
The connect (nBtn, & amp; MyPushButton: : mys, nBtn, & amp; MyPushButton: : response);
Consult everybody, thank you!
CodePudding user response:
Perform guanbi (), the MainWindow haven't into the event loop (in the constructor of MainWindow)
CodePudding user response:
reference 1/f, non Yi response: perform guanbi (), the MainWindow haven't into the event loop (in the constructor of MainWindow) Why connection to connect (nBtn, & amp; MyPushButton: : mys, nBtn, & amp; MyPushButton: : response), the response can normal execution, not still in the constructor of MainWindow, nor enter the event loop? I am a novice, is not very good, thank you. CodePudding user response:
The connect function prototypes for: QMetaObject: : Connection QObject: : connect (const QObject * sender, Const char * signal, Const QObject * receiver, Const char * method, Qt: : ConnectionType type=Qt: : AutoConnection); The last parameter decision, the signal channel is directly connected or queue connection, the default value is Qt: : AutoConnection For Qt: : AutoConnection: 1, the sender and receiver attached thread is the same, is a direct connection 2, the sender and receiver attached thread is not the same, is a queue connection Attached thread can be understood as: in which thread new attachment which thread, eg: A * A=new A;//a clinging thread for thread1 Thread2 QThread *=new QThread; A - & gt; MoveToThread (thread2);//a thread itself into thread2 Direct connection: similar to directly call a function (immediately) The signal is placed in a queue queue connection:, receiver, such as free start connect (nBtn, & MyPushButton: : mys, NBtn, & MyPushButton: : response); Is a direct connection, myPushButton: : mys signal launch, will immediately perform myPushButton: : response connect (nBtn, & MyPushButton: : mys, This, & MainWindow: : close); Is also a direct connection, myPushButton: : mys signal launch, will immediately perform MainWindow: : close But in the MainWindow: : close, not just close the window, but send a QCloseEvent event to the MainWindow QCloseEvent will distribute in the event loop MainWindow, to filter the events in the event distribution (these are another story) QApplication a (arg c, argv); . //event loop //before the event loop to invoke the close invalid A.e xec (); CodePudding user response:
references 4 floor of Yi reply: connect function prototypes for: QMetaObject: : Connection QObject: : connect (const QObject * sender, Const char * signal, Const QObject * receiver, Const char * method, Qt: : ConnectionType type=Qt: : AutoConnection); The last parameter decision, the signal channel is directly connected or queue connection, the default value is Qt: : AutoConnection For Qt: : AutoConnection: 1, the sender and receiver attached thread is the same, is a direct connection 2, the sender and receiver attached thread is not the same, is a queue connection Attached thread can be understood as: in which thread new attachment which thread, eg: A * A=new A;//a clinging thread for thread1 Thread2 QThread *=new QThread; A - & gt; MoveToThread (thread2);//a thread itself into thread2 Direct connection: similar to directly call a function (immediately) The signal is placed in a queue queue connection:, receiver, such as free start connect (nBtn, & MyPushButton: : mys, NBtn, & MyPushButton: : response); Is a direct connection, myPushButton: : mys signal launch, will immediately perform myPushButton: : response connect (nBtn, & MyPushButton: : mys, This, & MainWindow: : close); Is also a direct connection, myPushButton: : mys signal launch, will immediately perform MainWindow: : close But in the MainWindow: : close, not just close the window, but send a QCloseEvent event to the MainWindow QCloseEvent will distribute in the event loop MainWindow, to filter the events in the event distribution (these are another story) QApplication a (arg c, argv); . //event loop //before the event loop to invoke the close invalid A.e xec (); Thank you for your detailed answer, then ask next: You answer the last mentioned above "in the MainWindow: : close, not just close this window, but send a QCloseEvent event to the MainWindow nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull