开发平台(Platform): (Ex: Win10, Linux, ...)
linux
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
QT
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
我想写能下棋的程式,但是在测试点击Button的功能时发现一个问题,
如果把button的定义(位置之类东西)跟connect都写在mainwindow,button就有反应,
但是如果把定义跟connect写在别的cpp档,button可以正常显示,但点了却没有反应。
预期的正确结果(Expected Output):
希望点了button之后能执行默认的动作
错误结果(Wrong Output):
点了没反应
程式码(Code):(请善用置底文网页, 记得排版,禁止使用图档)
.h档
#ifndef CHESS_H
#define CHESS_H
#include <QPushButton>
#include <QWidget>
#include <QObject>
#include "mainwindow.h"
class chess :QObject
{
Q_OBJECT
public:
chess(MainWindow *);
virtual ~chess();
QPushButton *button;
public slots:
void buttonClick();
private:
};
#endif // CHESS_H
.cpp
#include "chess.h"
#include <QDebug>
chess::chess(MainWindow *ptr) : QObject ()
{
button = new QPushButton(ptr);
button->setGeometry(565,602,70,70);
//button->setIcon(QIcon(":/new/prefix1/Xiangqi_cd1.svg.png"));
//button->setStyleSheet("border:none");
button->setIconSize(QSize(70, 70));
button->show();
connect(button, SIGNAL(clicked(bool)), this ,SLOT(buttonClick()));
}
chess::~chess()
{
}
void chess::buttonClick()
{
button->move(button->x() + 20, button->y());
}
mainwindow
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsScene>
#include <QWidget>
#include <QGraphicsView>
#include <QPixmap>
#include <QSize>
#include "chess.h"
#include "bing.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->scene = new QGraphicsScene(0 ,0 ,1240 ,870);
QPixmap
pix(":/new/prefix1/f16693bafedd4a723abac58fca9fab17_1_-1_art.jpg");
QSize picSize(850,850);
QPixmap scaledPixmap = pix.scaled(picSize, Qt::KeepAspectRatio);
scene->addPixmap(scaledPixmap);
ui->chessboard->setScene(this->scene);
chess b(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
补充说明(Supplement):
希望有大大能拨空回答我QAQ