简体   繁体   中英

QEvent Signal and Slot

I created a Qt Project and I added a QPushButton with a style I customized with QEvent and QMouseEvent. I added a slot to this button but it doesen't work. Here is a project like mine:

Header 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:

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

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

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

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

error: no matching function for call to '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())); to QObject::connect(&y, SIGNAL(clicked()), &w, SLOT(close())); . QObject::connect requires a pointer of the sender.

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())); .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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