简体   繁体   English

QEvent信号和插槽

[英]QEvent Signal and Slot

I created a Qt Project and I added a QPushButton with a style I customized with QEvent and QMouseEvent. 我创建了一个Qt项目,并添加了一个QPushButton,其样式是使用QEvent和QMouseEvent自定义的。 I added a slot to this button but it doesen't work. 我在此按钮上添加了一个插槽,但是它不起作用。 Here is a project like mine: 这是一个像我的项目:

Header 1: 标头1:

#ifndef MYQPUSHBUTTON_H
#define MYQPUSHBUTTON_H

#include <QPushButton>
#include <QPalette>

class myQPushButton : public QPushButton
{
Q_OBJECT
public:
explicit myQPushButton(QWidget *parent = 0);
~myQPushButton();
void enterEvent(QEvent* );
void leaveEvent(QEvent* );
void mousePressEvent(QMouseEvent *  );
void mouseReleaseEvent(QMouseEvent *  );

signals:

public slots:

private:
QPalette *palette;
};

#endif // MYQPUSHBUTTON_H

Header 2: 标头2:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "myQPushButton/myqpushbutton.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private:
Ui::MainWindow *ui;
myQPushButton *button;
};

#endif // MAINWINDOW_H

Source 1: 来源1:

#include "myqpushbutton.h"

myQPushButton::myQPushButton(QWidget *parent) :
QPushButton(parent)
{
setFixedSize(200,200);
setIconSize(QSize(200,200));
setIcon(QIcon("D:/clockbox/qt/clockbox/exit_button_wwin_norm.png"));
setFlat(true);

}

myQPushButton::~myQPushButton()
{

}

void myQPushButton::enterEvent(QEvent* )
 {
setIcon(QIcon("D:/clockbox/qt/clockbox/exit_button_wwin_enter.png"));
 }

 void myQPushButton::leaveEvent(QEvent* )
 {
setIcon(QIcon("D:/clockbox/qt/clockbox/exit_button_wwin_norm.png"));
 }

 void myQPushButton::mousePressEvent(QMouseEvent * )
 {
setIcon(QIcon("D:/clockbox/qt/clockbox/exit_button_wwin_pressed.png"));
 }

 void myQPushButton::mouseReleaseEvent(QMouseEvent * )
 {
setIcon(QIcon("D:/clockbox/qt/clockbox/exit_button_wwin_enter.png"));
 }

Source 2: 来源2:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
myQPushButton y;
QObject::connect(y, SIGNAL(clicked()), &w, SLOT(close()));

return a.exec();
}

Source 3: 资料来源3:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
button = new myQPushButton(this);
setCentralWidget(button);
}

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

I get this error on source 2: 我在源2上收到此错误:

error: no matching function for call to 'QObject::connect(myQPushButton&, const char*, MainWindow*, const char*)' QObject::connect(y, SIGNAL(clicked()), &w, SLOT(close())); 错误:没有匹配的函数用于调用'QObject :: connect(myQPushButton&,const char *,MainWindow *,const char *)'QObject :: connect(y,SIGNAL(clicked()),&w,SLOT(close()) ); ^ ^

I want this button to be an exit button. 我希望此按钮成为退出按钮。 Somebody can help me? 有人可以帮助我吗?

To overcome the compilation error you must change the line QObject::connect(y, SIGNAL(clicked()), &w, SLOT(close())); 为了克服编译错误,您必须更改QObject::connect(y, SIGNAL(clicked()), &w, SLOT(close())); to QObject::connect(&y, SIGNAL(clicked()), &w, SLOT(close())); QObject::connect(&y, SIGNAL(clicked()), &w, SLOT(close())); . QObject::connect requires a pointer of the sender. QObject :: connect需要发送者的指针。

But that is not the issue here, you are adding the signal from a button that is not actually added to the window (you create the button y that is connected to the close slot in main() but later on in MainWindow you create a new button and add it to the MainWindow as central widget. You should then remove the button from main() and do the QObject::connect call in MainWindow::MainWindow like this QObject::connect(button, SIGNAL(clicked()), this, SLOT(close())); . 但这不是这里的问题,您要添加实际上未添加到窗口的按钮的信号(您在main()创建连接到close插槽的按钮y ,但稍后在MainWindow创建一个新的按钮,并将其作为中央窗口小部件添加到MainWindow中。然后应从main()删除按钮,并在MainWindow::MainWindow进行QObject::connect调用,例如QObject::connect(button, SIGNAL(clicked()), this, SLOT(close()));

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    button = new myQPushButton(this);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(close()));
    setCentralWidget(button);
}

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

main: 主要:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM