【问题标题】:QT library QObject::connect: Cannot connect (null)::finished() to MainWindow::updateGUI() ErrorQT 库 QObject::connect: 无法连接 (null)::finished() 到 MainWindow::updateGUI() 错误
【发布时间】:2018-05-22 13:41:44
【问题描述】:

我目前正在学习 QT 库,因为它似乎是一个流行的 C++ GUI 库。以前,我使用 wxWidgets,在某些方面我确实喜欢。但是我一直发现QT在功能上给了我更多的选择,在某些方面和wxWidgets很相似。

无论如何,我收到一个奇怪的错误(这是这篇文章的标题),代码无法连接到我的插槽。起初,我以为我在声明我的插槽错误。然后,事实证明其中一个 QPushButton 对象正在干扰信号。

我有一个 Sequential Animation 对象,我在其中使用两个按钮,让它们淡出然后调整窗口大小。但是,由于出现此错误,所有动画都无法正常工作。直到我注释掉我的 QPushButton 对象。奇怪的是,这个对象还没有被分配空间!该对象是一个指针,我没有用 new 声明它,我收到了这个错误。

代码很简单,我将在此处发布 .h 和 .cpp 文件。我还将标记错误发生的位置以及给我问题的对象在哪里:

这是.h文件

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QSequentialAnimationGroup *p_group;

//    QParallelAnimationGroup *p_parallelGroup;

    QPushButton *p_button; 

    QPushButton *p_anotherButton;

    QPushButton *p_backButton; // This is the object that when commented out, removes the error

    QPushButton *p_nextButton;

    QPushButton *p_finishButton;

    QPropertyAnimation *p_sizeAnimation;

    QPropertyAnimation *p_fadeOut;

    QGraphicsOpacityEffect *p_opacityEffect;

 //   QPushButton *p_backButton;

    systemState p_GUIState;



    void changeGUIState(systemState nextState);

 private slots:
    void handleButton();

    void hideOpenButton();

    void hideNewButton();

    void hideBackButton();

    void hideFinishButton();

    void updateGUI();
};

这是 .cpp 文件中的代码:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QHBoxLayout *startingLayout = new QHBoxLayout();
    this->centralWidget()->setLayout(startingLayout);

    this->statusBar()->setSizeGripEnabled(false);

    this->statusBar()->setWindowTitle("Simulator");

    p_button = new QPushButton("New", this);
    p_anotherButton = new QPushButton("Open", this);


    this->centralWidget()->layout()->addWidget(p_button);
    this->centralWidget()->layout()->addWidget(p_anotherButton);

    this->setWindowTitle("Omni-FEM");
//    this->centralWidget()->layout()->children().at(1)
    this->setFixedSize(this->minimumSize());
//    p_anotherButton->setVisible(false);
//    this->setFixedSize(this->cenOpentralWidget()->size());

    connect(p_anotherButton, SIGNAL (released()), this, SLOT (handleButton()));
}


void MainWindow::changeGUIState(systemState nextState)
{
    switch(nextState)
    {
        case systemState::PHYSICS_CHOOSING:
        {
            switch(p_GUIState)
            {
                case systemState::ON_START_UP_STATE:
                {
                    this->setMaximumSize(QSize(1000, 1000));

                    if(p_group)
                        delete p_group;

                    p_group = new QSequentialAnimationGroup;

                    p_sizeAnimation = new QPropertyAnimation(this, "size");
                    p_sizeAnimation->setDuration(500);
                    p_sizeAnimation->setStartValue(this->size());
                    p_sizeAnimation->setEndValue(QSize(600, 600));

                    p_opacityEffect = new QGraphicsOpacityEffect(this);
                    QGraphicsOpacityEffect *test = new QGraphicsOpacityEffect(this);

                    p_button->setGraphicsEffect(test);
                    p_anotherButton->setGraphicsEffect(p_opacityEffect);

                    QPropertyAnimation *fadeTest = new QPropertyAnimation(test, "opacity");

                    p_fadeOut = new QPropertyAnimation(p_opacityEffect, "opacity");
                    p_fadeOut->setDuration(1000);
                    p_fadeOut->setStartValue(1);
                    p_fadeOut->setEndValue(0);
                    p_fadeOut->setEasingCurve(QEasingCurve::OutBack);

                    fadeTest = new QPropertyAnimation(test, "opacity");
                    fadeTest->setDuration(1000);
                    fadeTest->setStartValue(1);
                    fadeTest->setEndValue(0);
                    fadeTest->setEasingCurve(QEasingCurve::OutBack);

                    p_fadeOut->start();
                    fadeTest->start();
             //       p_group->addAnimation(p_fadeOut);
             //       p_group->addAnimation(fadeTest);
                    p_group->addAnimation(p_sizeAnimation);

                    connect(p_fadeOut, SIGNAL (finished()), this, SLOT (hideOpenButton()));
                    connect(fadeTest, SIGNAL (finished()), this, SLOT (hideNewButton()));

                    p_group->start();

                }
                    break;
                default:
                    break;
            }
        }
        break;
    default:
        break;
    }

    p_GUIState = nextState;
    connect(p_group, SIGNAL (finished()), this, SLOT (updateGUI())); // The Debugger throws the error on this line
}



// ----- Section is for Interrupts ------
void MainWindow::handleButton()
{
    changeGUIState(systemState::PHYSICS_CHOOSING);
}

void MainWindow::hideNewButton()
{
    p_button->setHidden(true);
}

void MainWindow::hideOpenButton()
{
    p_anotherButton->setHidden(true);
}

void MainWindow::hideBackButton()
{
}

void MainWindow::hideFinishButton()
{
}

void MainWindow::updateGUI()
{
    switch(p_GUIState)
    {
    case systemState::PHYSICS_CHOOSING:
        if(!p_finishButton)
        {
            p_finishButton = new QPushButton("Finish", this);
            this->centralWidget()->layout()->addWidget(p_finishButton);
        }
        break;
    default:
        break;
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

这里是枚举定义文件:

//! The system state enum that describes the state that the UI is in
/*!
    This is used to keep track of what state that the UI is in. This will
    effect how frames are drawn and what variables are initilized or accessed.
    Some variables are accessed only when the user is drawing on the canvas,
    for example.
*/
enum class systemState
{
    ON_START_UP_STATE,/*!< The default value for the enum. This is the state that the program is in when the user first opens the progam in order to load any default settings */
    INITIAL_START_UP,/*!< This is the state that the program is in when the user can choose either new or open. The startup screen */
    PHYSICS_CHOOSING,/*!< This is the state that the program is in when the user can choose the simulation they would like to run */
    MODEL_DEFINING,/*!< This is the state that the program is in when the user is drawing their geometry on the canvas */
    SIMULATING,/*!< This is the state that the program is in when the user is simulating their simulation */
    VIEWING_RESULTS/*!< This is the state that the program is in when the user is viewing the results of the simulation */
};

再次,我得到了错误:

QObject::connect: 无法连接 (null)::finished() 到 MainWindow::updateGUI()

虽然我有 QPushButton *p_button; 注释。当注释掉时,错误消失了。我尝试将 QPushButton 放在代码中的不同位置,但仍然收到相同的错误。

我对这个错误感到很困惑,因为我没有遇到任何类似的错误!任何帮助将不胜感激。

顺便说一句:在撰写本文时,我正在使用最新发布的 QT 版本。此外,对于两个按钮淡出的动画,我想将它们放在 QParallelAnimationGroup 中,但是 QParallelAnimationGroup 作为我的 QPushButton 正在发生同样的事情!因此,原因被注释掉了。如果您能提出解决这两个问题的建议,那就加倍积分!我认为他们的问题是一样的。

【问题讨论】:

  • 这个错误会把你扔到哪一行?
  • 应用程序运行时出现此错误。这不是编译错误
  • 但是使用调试器,它会告诉你错误发生在哪一行。
  • 当然,这是发生错误的那一行:connect(p_group, SIGNAL (finished()), this, SLOT (updateGUI())); (由于某种原因,我无法将其转换为代码格式)我将在源代码中对此进行标记。奇怪的是,错误可能是因为没有设置动画而发生的。但是 QPushButton 会如何干扰呢?
  • 大量不必要的代码很难追踪错误,而且说实话,你的代码不是我见过的最有序的,它消除了不必要的东西,它需要一个咖啡,并重组你的程序。

标签: c++ qt user-interface


【解决方案1】:

您收到此错误的原因是连接完成时 p_group 变量为空。也许changeGUIState(systemState)nextState != systemState::PHYSICS_CHOOSINGp_GUIState != systemState::ON_START_UP_STATE 调用。调试您的程序以找出答案!

另外我建议使用新的信号槽语法,例如:

connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue);

而不是

connect(sender, SIGNAL(valueChanged(QString, QString)), receiver, SLOT( updateValue(QString)));

为什么?最好的理由是:

编译时检查信号和槽是否存在、类型是否存在,或者 Q_OBJECT 是否缺失。

更多信息请参见https://wiki.qt.io/New_Signal_Slot_Syntax

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    相关资源
    最近更新 更多