PDA

View Full Version : Help in creating and monitoring widget all in code.



shawnlau
18th June 2018, 21:17
I found a tutorial about creating widgets and layouts without using the Qt designer:
http://www.bogotobogo.com/Qt/Qt5_LayoutNotUsingDesigner.php

What I'm stuck on is how to connect the buttons to slots.
The following code compiles, but it gets all sorts of linking errors.I've commented out the code that causes the linking errors.

Any help getting this to work would be appreciated.

.pro file is:


QT += widgets

SOURCES += \
main.cpp


Source is:


#include <QApplication>
#include <QPushButton>
#include <QtCore>
#include <QtGui>
#include <QDebug>
#include <QObject>

//class ButtonWatcher : public QObject{
// Q_OBJECT
//public:
// ButtonWatcher(QObject *parent = 0);
//public slots:
// void buttonPressed();

//};
//ButtonWatcher::ButtonWatcher(QObject *parent) : QObject(parent){

//}
//void ButtonWatcher::buttonPressed(){
// qDebug()<<"Button clicked";
//}

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton *b = new QPushButton("Click me");
// ButtonWatcher *button = new ButtonWatcher();
// QObject::connect(b,SIGNAL(clicked(bool)),button,SL OT(buttonPressed()));
b->show();
return app.exec();

}



Here are the linking errors:
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl ButtonWatcher::metaObject(void)const " (?metaObject@ButtonWatcher@@UEBAPEBUQMetaObject@@X Z)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl ButtonWatcher::qt_metacast(char const *)" (?qt_metacast@ButtonWatcher@@UEAAPEAXPEBD@Z)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __cdecl ButtonWatcher::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@ButtonWatcher@@UEAAHW4Call@QMetaObje ct@@HPEAPEAX@Z)
debug\WithoutDesigner.exe:-1: error: LNK1120: 3 unresolved externals

high_flyer
18th June 2018, 21:35
The problem is that you didn't run moc on your ButtonWhatcher.
Put the ButtonWatcher in its own header and include that header in main.
Then run qmake again, and rebuild.
It should link then.

shawnlau
18th June 2018, 22:07
That worked. Thank you.