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. Help needed: Creating Object from classes

Help needed: Creating Object from classes

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 913 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    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.

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

    V 1 Reply Last reply
    0
    • SGaistS SGaist

      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.

      V Offline
      V Offline
      veixeros
      wrote on last edited by
      #5

      @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 !

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

        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.

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

        V 1 Reply Last reply
        0
        • SGaistS SGaist

          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.

          V Offline
          V Offline
          veixeros
          wrote on last edited by
          #7

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

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

            What will that button do ?

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

            V 1 Reply Last reply
            0
            • SGaistS SGaist

              What will that button do ?

              V Offline
              V Offline
              veixeros
              wrote on last edited by
              #9

              @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' "

              jsulmJ JonBJ 2 Replies Last reply
              0
              • V veixeros

                @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' "

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #10

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

                MainWindow::

                remove it...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • V veixeros

                  @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' "

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #11

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

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    veixeros
                    wrote on last edited by veixeros
                    #12

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

                    JonBJ 1 Reply Last reply
                    0
                    • V veixeros

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

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #13

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

                      1 Reply Last reply
                      1
                      • V Offline
                        V Offline
                        veixeros
                        wrote on last edited by
                        #14

                        @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'

                        JonBJ 1 Reply Last reply
                        0
                        • V veixeros

                          @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'

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #15

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

                          1 Reply Last reply
                          3

                          • Login

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