如何在静态方法中与QPushButton交互

how to interact with QPushButton in a static Method

本文关键字:QPushButton 交互 静态方法      更新时间:2023-10-16

我有2个按钮的弹出窗口,当我使用

时,我想在静态方法中使用这些按钮的SIGNAL和SLOT
connect(allbox->getAcceptButton(),SIGNAL(clicked()),this,SLOT(dosmt());

程序崩溃了。我知道怎么解决这个问题吗?谢谢你的帮助

你必须写一个插槽调用静态方法

您已经问了一个类似的问题,答案是相同的:您不能在静态方法中使用this,因此您将不得不传递一个具有dosmt()插槽的对象。

 class Test: public QObject
 {
     Q_OBJECT
 ...
 public:
     static void testMethod();
 public slots:
     void testSlot();
 };
Test::Test()
{
    QObject::connect(button, SIGNAL(clicked(), this, SLOT(testSlot()));
}
Test::testSlot()
{
    Test::testMethod();
}

只是一个简短的例子,你必须连接一个信号与插槽,因为它是由Qt文档http://doc.qt.digia.com/qt/signalsandslots.html