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_HPlayTable.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_HFunction 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.
-
Hi,
Why is your PlayTable a MainWindow ?
And from your inheritance tree, why are Player and Cards PlayTable subclasses ? -
Hi,
Why is your PlayTable a MainWindow ?
And from your inheritance tree, why are Player and Cards PlayTable subclasses ? -
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.
-
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.
-
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.
-
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.
-
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' "
-
@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' "
-
@JonB @jsulm
removingMainWindow::or addingthis->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]->cardButtonto it ? I just thought there would be an easier way to add widgets to the ui from within other classes. -
@JonB @jsulm
removingMainWindow::or addingthis->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]->cardButtonto 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 plainui->horizontalLayout->...you do not. At least assuming you have ahorizontalLayout....I think you'd better show what you have now. the last I saw you had
class playTable : public MainWindowand
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
playTableorMainWindowclass is responsible for managing its window. I think I would either create instances of other classes to the window inplayTable/MainWindowitself 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_HplayTable.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_HFor 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'
-
@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_HplayTable.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_HFor 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 accessuifromclass playTablenow that you have stopped that from inheriting fromMainWindow!uiis a member ofMainWindow, created automatically by theuiccompiler from your design.uifile, so neitheruinorthis->uiis 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
playTableclass is supposed to be/do, as a functional unit. It does not derive from anyQWidget, 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->horizontalLayoutto its constructor or methods to give it access to the UI (not that I'm advising that). You won't be able to passui(norMainWindow) 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.