[問題] QT button跟connect的問題
開發平台(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
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.116.116.207
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1556718689.A.1EF.html
※ 編輯: Mysterydog (49.216.158.69), 05/01/2019 21:54:27
推
05/01 23:23,
5年前
, 1F
05/01 23:23, 1F
→
05/01 23:23,
5年前
, 2F
05/01 23:23, 2F
→
05/02 00:37,
5年前
, 3F
05/02 00:37, 3F
→
05/02 00:37,
5年前
, 4F
05/02 00:37, 4F
→
05/02 00:37,
5年前
, 5F
05/02 00:37, 5F
→
05/02 00:37,
5年前
, 6F
05/02 00:37, 6F
推
05/02 00:54,
5年前
, 7F
05/02 00:54, 7F
→
05/02 00:54,
5年前
, 8F
05/02 00:54, 8F
→
05/02 01:12,
5年前
, 9F
05/02 01:12, 9F
→
05/02 01:15,
5年前
, 10F
05/02 01:15, 10F
→
05/02 01:15,
5年前
, 11F
05/02 01:15, 11F
推
05/02 01:22,
5年前
, 12F
05/02 01:22, 12F
→
05/02 01:23,
5年前
, 13F
05/02 01:23, 13F
→
05/02 01:23,
5年前
, 14F
05/02 01:23, 14F
→
05/02 01:52,
5年前
, 15F
05/02 01:52, 15F
→
05/02 01:53,
5年前
, 16F
05/02 01:53, 16F
→
06/05 12:21,
5年前
, 17F
06/05 12:21, 17F