Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Stuck: How to create a button.
QtWS25 Last Chance

Stuck: How to create a button.

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 625 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Whitney
    wrote on last edited by ambershark
    #1

    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 API

    moc_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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      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) { }
      };
      

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved