Reputation: 3673
I'm trying to put a title just before a QSLider so the user know what the QSlider is for. I have 2 QSlider in a QDialog Box set on a QVBoxLayout like this :
QSlider * slider1 = new QSlider(Qt::Horizontal);
QSlider * slider2 = new QSlider(Qt::Horizontal);
QDialog * opts = new QDialog;
QVBoxLayout * layout = new QVBoxLayout;
layout->addWidget(Qstring("Label for Slider 1"));
layout->addWidget(slider1);
layout->addWidget(QString("Label for Slider 2"));
layout->addWidget(slider2);
opts->setLayout(layout);
opts->show();
Now the sliders works just fine, but I can't show the text. And I've found absolutely nothing about a thing like this, whatsoever. How can I just show a String in a QVBoxLayout ?
Upvotes: 0
Views: 1212
Reputation: 5978
You can't directly add QString
like that, since QString
is just a convenient class for string operation and it's not responsible for UI display.
You need a instance of QLabel
to hold your QString
:
QLabel *label_1 = new QLabel(this);
label_1->setText("Label for Slider 1");
layout->addWidget(label_1);
layout->addWidget(slider1);
In addition, you may consider using nested layout: create a new horizontal layout that holds one QLabel
and one QSlider
, then add the layout into your dialog layout.
Here is the code:
QSlider * slider1 = new QSlider(Qt::Horizontal);
QSlider * slider2 = new QSlider(Qt::Horizontal);
QDialog * opts = new QDialog;
QVBoxLayout * layout = new QVBoxLayout; // layout for Dialog itself
QHBoxLayout * layout_1 = new QHBoxLayout(this); // layout for slider1 & label1
QHBoxLayout * layout_2 = new QHBoxLayout(this); // layout for slider2 & label2
QLabel *label_1 = new QLabel(this);
QLabel *label_2 = new QLabel(this);
label_1->setText("Label for Slider 1");
label_2->setText("Label for Slider 2");
layout_1->addWidget(label_1);
layout_1->addWidget(slider1); // [Label_1][Slider1]
layout_2->addWidget(label_2);
layout_2->addWidget(slider2); // [Label_2][Slider2]
layout->addLayout(layout_1);
layout->addLayout(layout_2);
opts->setLayout(layout);
opts->show();
P.S. I strongly recommend you using designer to deploy your widget layouts.
Upvotes: 2