Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help needed: Creating Object from classes



  • I have the following situation: I got the following inheritance line:
    MainWindow->PlayTable->Card,Player.

    When I create a new PlayTable from inside MainWindow, a new MainWindow is also created. I know it because when I start the Programm there is background music played. When the PlayTable is created, the music starts from the beginning overlapping the current music. When I create a new Card inside PlayTable, another PlayTable and another MainWindow is created.

    I think I messed something up with the parenting / inheritance. Could you give me a hint ?

    Here are the headers and the function calls for creating the new objects..

    Mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMediaPlayer>
    #include <QVideoWidget>
    #include <QList>
    #include <QString>
    
    class playTable;
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        Ui::MainWindow* ui;
    
    private slots:
    
    
        void on_exit_clicked();
    
        void on_start_clicked();
    
        void on_settings_clicked();
    
        void on_instructions_clicked();
    
        void hideMenue();
    
        void on_back_clicked();
    
        void showMenue();
    
    private:
    
        QMediaPlayer* player;
        QMediaPlayer* music;
        QVideoWidget* vw;
        QMediaObject* Intro;
        QString line;
        QMediaPlaylist* playlist;
    
    };
    #endif // MAINWINDOW_H
    

    PlayTable.h

    #ifndef PLAYTABLE_H
    #define PLAYTABLE_H
    
    
    #include <mainwindow.h>
    #include <QList>
    
    class player;
    class card;
    
    class playTable : public MainWindow
    {
    public:
        playTable();
    
        void dealCards();
    
        void createDeck();
    
        void shuffleDeck();
    
        QList<card*> deck;
    
    };
    
    #endif // PLAYTABLE_H
    

    Function call inside MainWindow for creating a playtable:

    void MainWindow::on_start_clicked()
    {
        playTable* game = new playTable();
    
        hideMenue();
    }
    

    Can you guys already see my mistake ? Btw everything compiles without mistakes.


  • Lifetime Qt Champion

    Hi,

    Why is your PlayTable a MainWindow ?
    And from your inheritance tree, why are Player and Cards PlayTable subclasses ?



  • @SGaist I want to set MainWindow as a parent of Playtable so I can manipulate the ui from within playtable. Same goes for card and player


  • Lifetime Qt Champion

    Then you are starting on the wrong foot. Why should any card have to be a full PlayTable ?

    Each of these should have an API that you can connect when needed to do something.

    Also, just because PlayTable is a MainWindow it does not mean that you are going to control anything from the instance that created it.



  • @SGaist Oh I see my problem now.. yes that makes no sense. I thought its the right way because there were no errors. But what do you mean by API ? How does this connection look like ? Thanks for your help btw !


  • Lifetime Qt Champion

    An API is the interface you give to your classes so you can use them.

    As for connection, I am referring to Qt's Signals and Slots concepts.



  • @SGaist Okay I read through the documention but I don't understand how I can use this in my case. Could you maybe give me a code snippet for my situation ? I create a pushButton in playtable. then I want this pushbutton to be added to the ui by a function within playtable.


  • Lifetime Qt Champion

    What will that button do ?



  • @SGaist i want to call thais function inside playtable:

    MainWindow::ui->horizontalLayout->addWidget(deck[i]->cardButton, 0, 0);
    

    but it says "invalid use of non-static data member 'ui' "


  • Qt Champions 2019

    @veixeros said in Help needed: Creating Object from classes:

    MainWindow::

    remove it...



  • @veixeros
    ... as @jsulm has said, or you can replace MainWindow:: by this->, to understand what is going on. The important thing to grasp is the difference between this->ui->... versus your MainWindow::ui->.



  • @JonB @jsulm
    removing MainWindow:: or adding this-> doesn't work. I also removed the inheritance, so now playtable, card and player are independend classes.

    Would it work if I create a function inside MainWindow with QPushButton as an argument and simply pass the deck[i]->cardButton to it ? I just thought there would be an easier way to add widgets to the ui from within other classes.



  • @veixeros said in Help needed: Creating Object from classes:

    removing MainWindow:: or adding this-> doesn't work

    Define "Doesn't work". When you had

    MainWindow::ui->horizontalLayout->...
    

    you got compilation error "invalid use of non-static data member 'ui' ". With this->ui->horizontalLayout->... or plain ui->horizontalLayout->... you do not. At least assuming you have a horizontalLayout....

    I think you'd better show what you have now. the last I saw you had

    class playTable : public MainWindow
    

    and

    void MainWindow::on_start_clicked()
    {
        playTable* game = new playTable();
    

    which is all over the place.

    I just thought there would be an easier way to add widgets to the ui from within other classes.

    Depends what you're doing, but I don't think I would add anything to the ui "from other classes". Your playTable or MainWindow class is responsible for managing its window. I think I would either create instances of other classes to the window in playTable/MainWindow itself and add from there, or possibly pass a parameter from there to the other class for where I wanted a new widget to be added....



  • @JonB
    Sorry for being a little unclear. That's the situation now:

    MainWindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMediaPlayer>
    #include <QVideoWidget>
    #include <QList>
    #include <QString>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        Ui::MainWindow* ui;
    
    private slots:
    
    
        void on_exit_clicked();
    
        void on_start_clicked();
    
        void on_settings_clicked();
    
        void on_instructions_clicked();
    
        void hideMenue();
    
        void on_back_clicked();
    
        void showMenue();
    
    private:
    
        QMediaPlayer* player;
        QMediaPlayer* music;
        QVideoWidget* vw;
        QMediaObject* Intro;
        QString line;
        QMediaPlaylist* playlist;
    };
    #endif // MAINWINDOW_H
    

    playTable.h:

    #ifndef PLAYTABLE_H
    #define PLAYTABLE_H
    
    
    #include <mainwindow.h>
    #include <QList>
    #include <card.h>
    
    
    class playTable
    {
    public:
        playTable();
    
        void dealCards();
    
        void createDeck();
    
        void shuffleDeck();
    
        QList<card*> deck;
    
    };
    
    #endif // PLAYTABLE_H
    

    For my understanding class MainWindow and class playTable are not connected now (besides #include).

    Now for the playTable.cpp:

    #include "playtable.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QList"
    #include "QFile"
    #include "QRandomGenerator"
    #include "card.h"
    
    playTable::playTable()
    {
    
    }
    
    void playTable::shuffleDeck()
    {
        for (int i = 0;i < 36;i++)
        {
            ui->horizontalLayout->addWidget(deck[i]->cardButton, 0, 0);
        }
    }
    

    Now these are my "doesn't work"s :

    ui->horizontalLayout->addWidget(deck[i]->cardButton, 0, 0);
    

    gets me use of undeclared identifier 'ui'

    this->ui->horizontalLayout->addWidget(deck[i]->cardButton, 0, 0);
    

    gets me no member called 'ui' in 'playTable'



  • @veixeros
    Of course it won't work trying to access ui from class playTable now that you have stopped that from inheriting from MainWindow! ui is a member of MainWindow, created automatically by the uic compiler from your design .ui file, so neither ui nor this->ui is going to find it.

    That's what I meant by it is important you understand why/where it does/does not work. I don't wish to sound hard on you, but you really need to understand C++ classes/inheritance/members before you'll get anywhere with Qt, just slapping things in doesn't help. Please consider pausing and doing a bit of reading on classes, instances, inheritance etc. before you try to proceed further.

    I don't understand what your playTable class is supposed to be/do, as a functional unit. It does not derive from any QWidget, so it's not a widget/visual element. Without that it's hard to advise you what to correct to make things work. As it stands, you would need to pass a parameter of, say, ui->horizontalLayout to its constructor or methods to give it access to the UI (not that I'm advising that). You won't be able to pass ui (nor MainWindow) because that class is private to your main window.

    I'm perhaps not the best person to take you forward from here, perhaps someone else will be. I do think you should do a little learning about classes and thinking about your design architecture to progress. Best wishes.


Log in to reply