Unsolved How to make variable objects?
-
Hi
MPlayer is a local variable and will not live after Main_Game ends.
so you might need to make it a member of Gameplay -
@mrjj said in How to make variable objects?:
Hi
MPlayer is a local variable and will not live after Main_Game ends.
so you might need to make it a member of GameplayI tried to define it as a member of Gameplay. But got an error message. What does that mean?
class Gameplay : public QDialog
{
Q_OBJECTpublic:
MPlayer p1, p2;
//...
}C:\Users\mfdsr\Documents\MasterZMDJ\gameplay.h:36: error: field 'p1' has incomplete type 'MPlayer'
MPlayer p1, p2; -
Nevermind, I managed to make p1 and p2 global variables.
But still those QLabels don't appear...
-
@MasterBlade
Dont use global variables. its not good with Qt.Anyway, the "incomplete type 'MPlayer'" means you forgot to include
MPlayer.h and hence it didnt know it.Well, its hard to guess. What do you then do with MPlayer ? what do u insert that into ?
Or is it a window? -
@mrjj said in How to make variable objects?:
@MasterBlade
Dont use global variables. its not good with Qt.Anyway, the "incomplete type 'MPlayer'" means you forgot to include
MPlayer.h and hence it didnt know it.Well, its hard to guess. What do you then do with MPlayer ? what do u insert that into ?
Or is it a window?Actually MPlayer is just a class. I declared it in gameplay.h . It stored info for players.
-
@MasterBlade
So what do u see in dialog ?
None of the labels are shown ?
I cant guess what goes wrong.Did you try to insert just 1 label and see ?
-
@MasterBlade You need to call show() on your labels if you want to see them...
-
@jsulm said in How to make variable objects?:
@MasterBlade You need to call show() on your labels if you want to see them...
I have an embarrassing problem. Those Labels are shown outside of the current window. And their positions are incorrect. I moved them to the relative position of current window. But somehow they appeared outside.
-
@MasterBlade Why don't you simply use layouts? And I guess you did not set parent on this labels, that's why they are outside of the mainwindow.
-
@jsulm said in How to make variable objects?:
@MasterBlade Why don't you simply use layouts? And I guess you did not set parent on this labels, that's why they are outside of the mainwindow.
Sorry I didn't go to this forum recently. I set parent label and everything works just fine.
I am using a customized label to store more information. That's why I created them in functions.
Now I am creating context menus for these labels. It looks like this. hands_s[i] are pointers for those 10 labels.
connect(hands_s[i], &Gameplay::customContextMenuRequested, this, &Gameplay::HandContextMenu);
My problem is, how do I know which label I am right-clicking? Do I have to connect them with 10 different functions?
-
@MasterBlade
You can use sender() to get the QLabel;QLabel* lab= qobject_cast<QLabel*>(sender()); if( lab ) { ... }
-
@mrjj said in How to make variable objects?:
@MasterBlade
You can use sender() to get the QLabel;QLabel* lab= qobject_cast<QLabel*>(sender()); if( lab ) { ... }
That does work! Many thanks!!