释放 QPushbutton 时未检测到插槽

Slot is not detected when QPushbutton is released

我目前正在制作一个游戏的主菜单,但是当点击按钮时它似乎没有调用相关的插槽(按钮没有做任何事情)。我试图将按钮移动到一个线程以确保没有任何东西阻止它立即进入 运行。我已经编写了一个按钮工作的更简单的程序,所以看起来问题不在于按钮或信号本身。因此它一定是有什么东西阻止了它 运行? 源文件给出为:

    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QObject>
    #include <QBrush>
    #include"game.h"
    #include <QMediaPlayer>
    #include <QImage>
    #include<QGraphicsPixmapItem>
    #include <QPixmap>
    #include<QGraphicsItem>
    #include <QPushButton>
    #include <QLineEdit>
    #include <QPalette>
    #include <QMessageBox>
    #include <QLabel>
    #include <QComboBox>

    Game::Game(QWidget*parent)
    {
    //adding the background

    QGraphicsScene *scene=new QGraphicsScene();//to create a new graphics scene
    scene->setSceneRect( 0,0,1366,768);
    setBackgroundBrush(QBrush(QImage(":/images/Back_1_withlogo.jpg")));
    setScene(scene);
    show();
    setFixedSize(1366,768);

    //adding logo
        //setPixmap(QPixmap(":/images/Logo.png"));
    //adding the soundtrack

    music=new QMediaPlayer();
    music->moveToThread(&MusicThread);
    music->setMedia(QUrl("qrc:/sounds/Surreal-Game-Menu.mp3"));
    music->play();
    MusicThread.start();


    //add GUI
    this->setFixedSize(1366,768);
    this->move(-7,-5);
    //start button and username edit box
    btnStart = new QPushButton(this);
    btnStart->moveToThread(&btnThread);

    UserName = new QLineEdit(this);
    UserName->setFixedSize(100,25);
    UserName->move(300,300);
    UserName->show();
    UserName->setMaxLength(12);

    btnStart->setText("Start");
    btnStart->setFixedSize(100,30);
    btnStart->show();
    btnStart->move(300,600);
    connect(btnStart, SIGNAL (released()),this, SLOT (btnStart_clicked()));
    btnThread.start();

    //label for username edit box
    QLabel *LblUsername= new QLabel(this);
    LblUsername->setStyleSheet("QLabel { color : white; }");
    LblUsername->setFixedSize(100,30);
    LblUsername->move(230,300);
    LblUsername->setText("Username:");
    LblUsername->show();

    QLabel *lblGameMode = new QLabel(this);
    lblGameMode->setStyleSheet("QLabel { color : white; }");
    lblGameMode->setFixedSize(100,30);
    lblGameMode->move(190,450);
    lblGameMode->setText("Select Game mode:");
    lblGameMode->show();

    //combobox to select players
    GameMode=new QComboBox(this);
    GameMode->move(300,450);
    GameMode->setFixedSize(100,30);
    QStringList Modelist=(QStringList()<<"singleplayer"<<"co-op"<<"multiplayer (3 players)");
    GameMode->addItems(Modelist);
    GameMode->show();
    }

    void Game::btnStart_clicked()
     {
     if (UserName->text()==NULL)
     {
         QMessageBox messageBox;
         messageBox.critical(0,"Error","Please insert username!");
         messageBox.setFixedSize(500,200);
     }
     else
     {
         numPlayers = GameMode->currentIndex();
         SUsername= UserName->text();
         UserName->setText("works");
     }
     }

header 代码如下:

    #ifndef GAME_H
    #define GAME_H

    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QWidget>
    #include <QPushButton>
    #include <QLineEdit>
    #include <QMediaPlayer>
    #include <QThread>
    #include <QComboBox>


    class Game:public QGraphicsView
    {
    public:
        Game(QWidget*parent=0);
        QGraphicsScene*scene;
    private slots:
        void btnStart_clicked();
    private:
        QPushButton *btnStart;
        QLineEdit *UserName;
        QMediaPlayer *music;
        QThread MusicThread;
        QString SUsername;
        QThread btnThread;
        QComboBox *GameMode;
        int numPlayers;

    };
    #endif // GAME_H

我收到以下错误:

    QObject::connect: No such slot QGraphicsView::btnStart_clicked() in game.cpp:57

添加一个Q_OBJECT宏,如下所示:

class Game:public QGraphicsView
    {
    Q_OBJECT
    public:
        Game(QWidget*parent=0);
        QGraphicsScene*scene;
    private slots:
        void btnStart_clicked();
    private:
        QPushButton *btnStart;
        QLineEdit *UserName;
        QMediaPlayer *music;
        QThread MusicThread;
        QString SUsername;
        QThread btnThread;
        QComboBox *GameMode;
        int numPlayers;

    };

这个宏帮助编译器确定 class 正在使用信号和槽机制。基本上它在 MOC(元对象编译器)文件中创建一个已定义信号和槽的条目。 通过下面 link 了解信息: http://www.bogotobogo.com/Qt/Qt5_Q_OBJECT_Macro_Meta_Object.php

QObject::connect (btnStart, &QPushButton::clicked, this, &Game::btnStart_clicked);可能有效...如果您想使用释放事件,请尝试覆盖 virtual void mouseRelease 或 eventFilter。

当然还有……Q_OBJECT marco。