Skip to main content
 首页 » 编程设计

qt中QPushButton 连接到祖 parent

2025年01月19日82傻小

我的应用程序有一个主窗口,当单击按钮时,我有一个用于打开弹出窗口的插槽:

void MainWindow::slotContinue() 
{ 
    PopupWindow *pop = new PopupWindow(this); 
    pop->show(); 
} 

我将 this 传递给弹出窗口,以便弹出窗口中的按钮可以连接到主窗口的插槽。

PopupWindow::PopupWindow(QWidget *parent) : 
    QWidget(parent) 
{ 
    setFixedSize(360, 100); 
 
    contButton = new QPushButton("Continue", this); 
    contButton->setGeometry(140, 60, 80, 30); 
 
    connect(contButton, SIGNAL(clicked()), parent, SLOT(slotCalibrate())); 
    connect(contButton, SIGNAL(clicked()), this, SLOT(close())); 
} 

弹出窗口永远不会出现。出现继续按钮,但它是主窗口的一部分。按钮的功能很好。单击它后,slotCalibrate() 被成功调用并且按钮消失,但我无法弄清楚为什么它是应该是其祖 parent 的 child 。

如果我不将 this 传递给弹出式构造函数,则会出现窗口,但我无法将继续按钮连接到 slotCalibrate()

请您参考如下方法:

PopupWindow 类继承自 QWidget,因此如果它有父窗口,默认情况下它不是一个窗口,它将成为该父窗口的一部分,您需要将其设置为是一个窗口:

void MainWindow::slotContinue() 
{ 
    PopupWindow *pop = new PopupWindow(this); 
    pop->setWindowFlags(Qt::Window); 
    pop->show(); 
}