Stuck: How to create a button.
-
I have created a function that will create a button from the main class i create a pointer to an object and a pointer to a reference for the app which i pass to the createButton function in the Button.cpp file from the main.cpp file
I wanted to use the same class for the callbacks so the Q_OBJECT macro looks promising so i put that macro into my Button Class.
I'm stuffing the extras of my graphics into the pressed and released functions i want to create and do the graphics handling through there.
The Button.h file extending QPushButton
#include <QPushButton> #include <QObject> #include <QApplication> #include <iostream> class Button : QPushButton { Q_OBJECT signals: void pressed (); void released (); private slots: void clickHandle(); void pressHandle(); public: Button(); void createButton(QApplication &app); void toggled ( bool checked ); void customContextMenuRequested ( const QPoint &pos ); void destroyed ( QObject * obj = 0 ); ~Button(); };
and the Button.cpp file in the createButton function
QPushButton button; button.setText("Hello World"); button.setGeometry(0,0,50,75); button.connect(&button, SIGNAL(released()), &app, SLOT(Button::releaseHandle())); button.connect(&button, SIGNAL(pressed()), &app, SLOT(Button::pressHandle())); button.show();
in the main.cpp file
Button *power = new Button(); QApplication &App = a; QApplication *AppPoint = &App; power->createButton(*AppPoint);
I'm getting linking errors showing as this.
button.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Button::metaObject(void)const " (? metaObject@Button@@UEBAPEBUQMetaObject@@XZ) button.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl Button::qt_metacast(char const *)" (? qt_metacast@Button@@UEAAPEAXPEBD@Z)
button.obj:-1: error: LNK2001: unresolved external symbol "public: virtual
int __cdecl Button::qt_metacall(enum QMetaObject::Call,int,void * *)" (?
qt_metacall@Button@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
"The presence of the Q_OBJECT macro marks the class for inclusion in Qt's meta-object system. If you want your class to have its own identity in this meta-object system, you must put the Q_OBJECT macro into it (and make sure it is directly or indirectly derived from QObject, naturally).In your case of cBaseObject and cEnhancedbaseObject, if cEnhancedbaseObject does not include the Q_OBJECT macro, it will still work normally. However, as far Qt's meta-object system is concerned, objects of type cEnhancedbaseObject will be of meta-type cBaseObject. You can see that using such functions as myObject->metaObject()->className()." @Angew link for qt inheritance
Button->QPushButton->AbstractButton->QWidget->QObject AND QPaintDevice... does the double inheritance ruin my application for a Button Object. Perhaps i don't have the meta properly created.
I've updated with signals in the header file again run qmake and cleaned the project multiple times. As explained in the other thread this should rebuild the moc file and when adding Q_OBJECT the moc file should automate the code and generate the lines needed. Not going to delete those two files, cause that seems kinda stupid. Hmm?
Question is: Why isn't moc creating these lines, Q_OBJECT should be built when running, even qmake should remake the objects i assume. Going to read more about the moc compiler and qmake, maybe ill find something.
Goal/Question: Make wButton a custom button.
and grow screen like a bubble zD
to do searches of the Qt APImoc_button.obj:-1: error: LNK2005: "public: void __cdecl Button::pressed(void)" (?pressed@Button@@QEAAXXZ) already defined in button.obj
it is already defined in the object, i have tried clearing and rebuilding the moc to no avail.
[@ambershark]: Added code tags to make it easier to read.
-
Hi,
Couple problems I see right off the bat. You don't take a parent in your constructor, and 2 you didn't inherit QPushButton publicly.
Try:
class Button : public QPushButton { Q_OBJECT public: explicit Button(QWidget *parent = nullptr) : QPushButton(parent) { } };
-
Hi,
To add to @ambershark, you're creating a QPushButton on the stack, so it will be destroyed before it's even shown.
Also, why all that stuff around your QApplication object ?