i created qt project , added qpushbutton style customized qevent , qmouseevent. added slot button doesen't work. here project 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 error on source 2:
error: no matching function call 'qobject::connect(myqpushbutton&, const char*, mainwindow*, const char*)' qobject::connect(y, signal(clicked()), &w, slot(close())); ^
i want button exit button. can me?
to overcome compilation error must change line qobject::connect(y, signal(clicked()), &w, slot(close()));
qobject::connect(&y, signal(clicked()), &w, slot(close()));
. qobject::connect requires pointer of sender.
but not issue here, adding signal button not added window (you create button y
connected close
slot in main()
later on in mainwindow
create new button , add mainwindow
central widget. should remove button main()
, qobject::connect
call in mainwindow::mainwindow
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(); }