释放 QPushbutton 时未检测到插槽

AO1114

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

    #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");
     }
     }

标题代码如下:

    #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;

    };

该宏帮助编译器确定该类正在使用信号和槽机制。基本上它在 MOC(元对象编译器)文件中创建一个定义的信号和插槽的条目。通过以下链接获取信息:http : //www.bogotobogo.com/Qt/Qt5_Q_OBJECT_Macro_Meta_Object.php

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

释放子模块时,如何解决“由于未释放依赖项而无法释放项目”?

来自分类Dev

“指针未释放”

来自分类Dev

线程释放资源时

来自分类Dev

检测退出时释放的C内存“泄漏”

来自分类Dev

_UItextContaineView内存未释放

来自分类Dev

尽管内存已被释放,Valgrind仍检测到内存泄漏

来自分类Dev

资源未释放

来自分类Dev

Qpushbutton未显示

来自分类Dev

Apache Httpclient连接未释放

来自分类Dev

在沙发床中检测到锁释放?

来自分类Dev

删除实例时未释放浮动IP

来自分类Dev

堆栈内存未释放

来自分类Dev

Valgrind检测到无法正确释放堆上已分配的内存

来自分类Dev

Android:检测后退按钮的释放

来自分类Dev

内存/资源未释放

来自分类Dev

内存/资源未释放

来自分类Dev

QTableWidget中QPushButton的Lambda插槽

来自分类Dev

C-释放数组时检测到堆损坏

来自分类Dev

使用calloc和更改的stdout释放内存时检测到堆损坏

来自分类Dev

检测到glibc-双重释放或损坏(!prev)

来自分类Dev

在沙发床中检测到锁释放?

来自分类Dev

删除实例时未释放浮动IP

来自分类Dev

未检测到传送带释放伪像

来自分类Dev

释放内存时出错

来自分类Dev

IIS在处理时未释放文件

来自分类Dev

指针未释放!无法解决

来自分类Dev

QPushButton:如何知道“释放”信号后是否会跟随“点击”信号?

来自分类Dev

执行free()时出错,并显示“ glibc检测到双重释放或损坏”

来自分类Dev

PySide/PyQt:检测 QPushButton 状态?