Creating QPushButtons in a class



  • 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,



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



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



  • @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?



  • 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. !#@$



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



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.