Unsolved How to make variable objects?
-
Hello, I have 10 similar QLabels . Their object names are from hand1 to hand10.
I want to choose an object from them using a variable, like
int i=5; ui->hand[i]->settext("");
How can I do this?
-
Hello,
A possible solution is to store each QLabel in a QMap.
// .h QMap<int, QLabel*> m_labels; //--------------------------- // .cpp //Init in constructor, after ui->setupUi(this) m_labels[1] = ui->hand1; m_labels[2] = ui->hand2; ... m_labels[10] = ui->hand10; //--------------------------- //And then, access it int i = 5; m_labels[i]->setText("Hello");
-
@Gojir4 @MasterBlade QVector would be faster than QMap in this case, or even std::array if number of elements is fixed.
-
@jsulm said in How to make variable objects?:
@Gojir4 @MasterBlade QVector would be faster than QMap in this case, or even std::array if number of elements is fixed.
I have exactly 10 objects. How can I link those objects with QVector or std::array ?
-
@MasterBlade Like shown in documentation: http://doc.qt.io/qt-5/qvector.html
QVector<QLabel*> labels; labels[0] = new QLabel(); ... labels[0]->setText("...");
-
@MasterBlade said in How to make variable objects?:
Their object names are from hand1 to hand10
if by "object name" you mean
QObject::objectName
then you can usefindChild<QLabel*>(QStringLiteral("hand%1").arg(i));
-
@VRonin said in How to make variable objects?:
@MasterBlade said in How to make variable objects?:
Their object names are from hand1 to hand10
if by "object name" you mean
QObject::objectName
then you can usefindChild<QLabel*>(QStringLiteral("hand%1").arg(i));
Actually I mean like Labels or Pushbuttoms. Do they work the same way?
-
@MasterBlade said in How to make variable objects?:
@VRonin said in How to make variable objects?:
@MasterBlade said in How to make variable objects?:
Their object names are from hand1 to hand10
if by "object name" you mean
QObject::objectName
then you can usefindChild<QLabel*>(QStringLiteral("hand%1").arg(i));
Actually I mean like Labels or Pushbuttoms. Do they work the same way?
If you create them by hand, inside your cpp/h file than you have to give them one explicitly via
setObjectName
, if you use the designer than all objects created inside of it get an unique objectName by default.But keep in mind
findChild
can quickly become a time extensive function expecially if you call it on the main parent, as it goes through all the children! -
Hi
Just as note.
Yes you can find any type of widget.
findChild<QLabel*> <<< just change type there. -
@jsulm said in How to make variable objects?:
@MasterBlade Like shown in documentation: http://doc.qt.io/qt-5/qvector.html
QVector<QLabel*> labels; labels[0] = new QLabel(); ... labels[0]->setText("...");
Hello, I tried the following code. There is no error while running. But those labels don't appear in the UI.
//hand is a public member of p1 //defined as QVector<QLabel*> hand; p1.hand.resize(10); for (uint8_t i = 0; i < 10; i++){ p1.hand[i] = new QLabel; p1.hand[i]->setText("Hand" + QString::number(i + 1)); p1.hand[i]->setObjectName("Hand" + QString::number(i + 1)); p1.hand[i]->resize(120, 170); p1.hand[i]->move(120 * i + 22, 522); }
-
Hi
But you dont give it a parent
or assign it to a layout.
so they wont show inside the existing mainwindow.What do you want to insert them in ?
you can try with
p1.hand[i] = new QLabel(this);if code inside the widget that should have them like mainwindow.
-
@mrjj said in How to make variable objects?:
Hi
But you dont give it a parent
or assign it to a layout.
so they wont show inside the existing mainwindow.What do you want to insert them in ?
you can try with
p1.hand[i] = new QLabel(this);if code inside the widget that should have them like mainwindow.
Hello, I want to insert them to the current window, gameplay.ui . These codes come from gameplay.cpp . I tried to merge those commands like this. Still those labels aren't displayed in the current window. What was wrong?
bool Gameplay::Main_Game()
{MPlayer p1, p2; p1.hand.resize(10); for (uint8_t i = 0; i < 10; i++){ p1.hand[i] = new QLabel("Hand" + QString::number(i + 1), this); p1.hand[i]->setObjectName("Hand" + QString::number(i + 1)); p1.hand[i]->resize(120, 170); p1.hand[i]->move(120 * i + 22, 522); }
//...
} -
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.