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. Creating QPushButtons in a class
QtWS25 Last Chance

Creating QPushButtons in a class

Scheduled Pinned Locked Moved General and Desktop
qwidgetqpushbutton
7 Posts 3 Posters 2.7k 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.
  • D Offline
    D Offline
    de_prodigy
    wrote on last edited by de_prodigy
    #1

    hi all-

    I'm developing solitaire and I need a card class that has a pointer to a QPushButton that will be added to the layout in the mainwindow and accessed from there. I'm not too familiar with Qt and my time working on this problem has given me this error: "QWidget: Must construct a QApplication before a QWidget". After some googling I'm no closer to my goal.

    Essentially what I want is:
    card.cpp

    #include <QtWidgets>
    
    class card
    {
    private:
        QPushButton* button;
    
    public:
        card(){
        button=new QPushButton();
        button->setText("test");
    }
        QPushButton* getButton() { return button; }
    };
    

    solitaire.cpp

    solitaire::solitaire(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::solitaire)
    {
        ui->setupUi(this);
    
        QGridLayout *vlay = new QGridLayout();    
        QWidget * wdg = new QWidget(this);
        wdg->setLayout(vlay);
        setCentralWidget(wdg);
    
        card test;
        vlay->addWidget(test.getButton(),1,1);
    }
    

    I want this to create a card called test and add its button to the layout at grid location 1,

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

      Normally in your main you have to create QApplication instance. Check any example.
      This has to be done before any QWidget derived class is instantiated.

      This message is displayed when there is an attempt to instantiate QWidget before QApplication.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        de_prodigy
        wrote on last edited by
        #3

        Hi Alex thank you for your reply.

        my main does what you say- creates a QApplication instance.

        #include "solitaire.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            solitaire w;
            w.show();
        
            return a.exec();
        }
        

        which means that I can create QPushButtons in solitaire right? which I can. However I want to create QPushButtons in a different class file. Do I have to create another QApplication in solitaire or?

        I'm missing something fundamental here...

        S 1 Reply Last reply
        0
        • D de_prodigy

          Hi Alex thank you for your reply.

          my main does what you say- creates a QApplication instance.

          #include "solitaire.h"
          #include <QApplication>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              solitaire w;
              w.show();
          
              return a.exec();
          }
          

          which means that I can create QPushButtons in solitaire right? which I can. However I want to create QPushButtons in a different class file. Do I have to create another QApplication in solitaire or?

          I'm missing something fundamental here...

          S Offline
          S Offline
          SysTech
          wrote on last edited by
          #4

          @de_prodigy

          Without seeing your code it is hard to say what is going on. You are including solitaire.h at the very start. Whats in it?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            de_prodigy
            wrote on last edited by de_prodigy
            #5

            Hey Systech

            here's my solitaire.h. it is business as usual.

            #define SOLITAIRE_H
            
            #include <QMessageBox>
            #include <QPainter>
            #include <algorithm>
            #include <ctime>
            #include <QtWidgets>
            #include <vector>
            #include "card.h"
            
            namespace Ui {
            class solitaire;
            }
            
            class solitaire : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit solitaire(QWidget *parent = 0);
                ~solitaire();
                void fillDeck(card d[]);
                QPushButton* paintCard(card c);
                void setupBoard();
                void flipCards();
            
            public slots:
                void processClick();
                void drawClicked();
            
            private:
                Ui::solitaire *ui;
            };
            
            #endif // SOLITAIRE_H
            

            lots of things are working correctly but my implementation requires a card class that has a QPushButton (which represents a card after giving it the appropriate icon). I can't find a way to create these push buttons inside my card objects. !#@$

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alex_malyu
              wrote on last edited by alex_malyu
              #6

              Look,

              There is Nothing wrong to have a class which would allocate QPushButton if it is really allocated after the QApplication instance.

              #include <QPushButton>

              int main(int argc, char *argv[])
              {
              QPushButton * p;

              QApplication a(argc, argv);
              p = new QPushButton();
              

              Above code works, Put last line above
              QApplication a(argc, argv);
              and you will get an error.

              Could you put a breakpoint in card constructor on the line:
              button=new QPushButton();

              and in the main at
              QApplication a(argc, argv);

              and make sure that you stop at second first.

              Another option is that you allocate card in not primary thread.
              Or you have somewhere static card instances which would want to be constructed before you enter main.
              And finally you may use incompatible libraries.

              Hope this helps.

              D 1 Reply Last reply
              0
              • A alex_malyu

                Look,

                There is Nothing wrong to have a class which would allocate QPushButton if it is really allocated after the QApplication instance.

                #include <QPushButton>

                int main(int argc, char *argv[])
                {
                QPushButton * p;

                QApplication a(argc, argv);
                p = new QPushButton();
                

                Above code works, Put last line above
                QApplication a(argc, argv);
                and you will get an error.

                Could you put a breakpoint in card constructor on the line:
                button=new QPushButton();

                and in the main at
                QApplication a(argc, argv);

                and make sure that you stop at second first.

                Another option is that you allocate card in not primary thread.
                Or you have somewhere static card instances which would want to be constructed before you enter main.
                And finally you may use incompatible libraries.

                Hope this helps.

                D Offline
                D Offline
                de_prodigy
                wrote on last edited by
                #7

                @alex_malyu

                Hi again Alex thanks again for giving me some assistance.

                I'm going to try to fiddle with my code with the information you've given me to hopefully make some progress. Just for full clarification the structure of my code is

                main.cpp creates a mainwindow solitaire which creates card objects. I want the QPushButtons to be part of the card objects member variables.

                Working with your advice now.

                1 Reply Last reply
                0

                • Login

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