I have a qlabel L inside a qwidget W. L is vertically and horizontally aligned.
When I resize W, L doesn't get centered.
Is this expected?
What's a good implementation to have L centered again?
To align text in a QLabel by calling QLabel::setAlignment works like expected for me.
Maybe you miss to add your Label to a Layout (so your label would automatically resized if your widget is resized). See also Layout Management. A minimal example:
#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QWidget>
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
QLabel* label=new QLabel("Hello World!");
label->setAlignment(Qt::AlignCenter);
QWidget* widget=new QWidget;
// create horizontal layout
QHBoxLayout* layout=new QHBoxLayout;
// and add label to it
layout->addWidget(label);
// set layout to widget
widget->setLayout(layout);
widget->show();
return app.exec();
}
Related
I'm trying to write an UI test (using Windows UI Automation) for an application that uses a QLabel to show an output to a user.
I create the label like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.setWindowTitle("MyWindowTitle");
auto centralWidget = new QWidget(&w);
centralWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
QVBoxLayout layout(centralWidget);
auto interrestingLabel = new QLabel(centralWidget);
QString valueCalculatedByApp = "1337";
interrestingLabel->setText(valueCalculatedByApp);
//interrestingLabel->setAccessibleName("MyAccessibleName");
layout.addWidget(interrestingLabel);
auto uninterrestingLabel = new QLabel(centralWidget);
uninterrestingLabel->setText("uninterrestingText");
layout.addWidget(uninterrestingLabel);
w.setCentralWidget(centralWidget);
w.show();
return a.exec();
}
Inspect.exe now shows the value "1337" as name of the widget:
Without accessible name
The problem with this would be that my UI test would need to figure out which one is the correct label.
If I uncomment setAccessibleName line, the widget is now identifiable, but I the text is no longer in visible in the properties.
With accessible name
Is there a way to read the text of a QLabel with an accessible name, or is there another way to make the QLabel identifieable while still being able to read the text?
I found a workaround:
Instead of a QLabel I use a QLineEdit. I set enabled to false so it is not selectable or editable (Like a QLabel) and use QSS to make it look like a QLabel:
#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QSizePolicy>
#include <QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setAttribute(Qt::AA_NativeWindows);
QMainWindow w;
w.setWindowTitle("MyWindowTitle");
auto centralWidget = new QWidget(&w);
centralWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
QVBoxLayout layout(centralWidget);
auto interrestingLabel = new QLineEdit(centralWidget);
QString valueCalculatedByApp = "1337";
interrestingLabel->setText(valueCalculatedByApp);
interrestingLabel->setAccessibleName("MyAccessibleName");
interrestingLabel->setEnabled(false);
interrestingLabel->setStyleSheet(
"border-style: none;"
"color: black;"
"background:transparent"
);
layout.addWidget(interrestingLabel);
auto uninterrestingLabel = new QLabel(centralWidget);
uninterrestingLabel->setText("uninterrestingText");
layout.addWidget(uninterrestingLabel);
w.setCentralWidget(centralWidget);
w.show();
return a.exec();
}
I am new with Qt (using Qt Creator) and the QProgressBar. I am interested in learning how to have a fixed text value (not the value of the progress bar) inside or adjacent to the left of a QProgressBar and have its font size scale according with the size of the progress bar.
For example:
or
I have considered using a QLabel but failed and I could not find any examples online.
Any code sample illustrating the solution for me to understand and learn from will be much appreciated.
If label inside the progressbar will do, then here is an example. This might not be exactly what you want, but it should send you in the right direction. I adjust the font size in the resize event. In this example the font size is calculated based on the size of the label, which is the same size as the progress bar.
#include <QApplication>
#include <QProgressBar>
#include <QWidget>
#include <QLabel>
#include <QLayout>
#include <QTimer>
class Widget : public QWidget
{
Q_OBJECT
QProgressBar progressBar;
QLabel *label;
public:
Widget(QWidget *parent = nullptr) : QWidget(parent)
{
progressBar.setRange(0, 100);
progressBar.setValue(20);
progressBar.setTextVisible(false);
progressBar.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
label = new QLabel(&progressBar);
label->setText("Hello World!");
setLayout(new QHBoxLayout);
layout()->addWidget(&progressBar);
}
protected:
void resizeEvent(QResizeEvent *)
{
label->resize(progressBar.size());
QFontMetrics fm(label->font());
float multiplier_horizontal = (float)label->width() / fm.width(label->text());
float multiplier_vertical = (float)label->height() / fm.height();
QFont font = label->font();
font.setPointSize(font.pointSize() * qMin(multiplier_horizontal, multiplier_vertical));
label->setFont(font);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "main.moc"
I would like to use a custom made QWidget in one row of a QFormLayout. The code below presents a form layout in which the first row has a a QLineEdit and the second line has a custom made widget:
The problem is that the custom made widget is not vertically aligned. How to align it vertically?
project.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CustomLineEdit
TEMPLATE = app
SOURCES += main.cpp
main.cpp
#include <QApplication>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QLineEdit>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QFormLayout *formLayout = new QFormLayout(&w);
QLineEdit *leUser = new QLineEdit;
QWidget *widget = new QWidget;
QHBoxLayout *hLayout = new QHBoxLayout(widget);
hLayout->addWidget(leUser);
hLayout->addWidget(new QPushButton);
formLayout->addRow("Text:", new QLineEdit);
formLayout->addRow("User:", widget);
w.show();
return a.exec();
}
This is because of margins. Just add followed line:
QHBoxLayout *hLayout = new QHBoxLayout(widget);
hLayout->setMargin(0); //this line
I need the way to setup widgets inside other widget with different layouts...
it is something like we have widget divided by one layout into two parts with labels, and
this widget have other widget inside with layout like on attached image
and we have only 4 widgets: main widget, label one widget, label two widget, button widget, and for button use one vertical and two horizontal stretch
Can some body point me to right way do it? Thanks.
Create QVBoxLayout, then add two QHBoxLayouts to it. In top QHBoxLayout add labels, in bottom one add stretch, button, stretch.
#include <QString>
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLocale>
int main(int argc, char** argv){
QApplication app(argc, argv);
QWidget widget;
QVBoxLayout* vLayout = new QVBoxLayout(&widget);
QHBoxLayout* topLayout = new QHBoxLayout();
QHBoxLayout* bottomLayout = new QHBoxLayout();
QLabel* label1 = new QLabel(QObject::tr("Label1"));
QLabel* label2 = new QLabel(QObject::tr("Label2"));
label1->setAlignment(Qt::AlignCenter);
label2->setAlignment(Qt::AlignCenter);
QPushButton* btn1 = new QPushButton(QObject::tr("The Button!!!!"));
topLayout->addWidget(label1);
topLayout->addWidget(label2);
bottomLayout->addStretch();
bottomLayout->addWidget(btn1);
bottomLayout->addStretch();
vLayout->addLayout(topLayout);
vLayout->addLayout(bottomLayout);
widget.show();
return app.exec();
}
Here is an equivalent extracted code:
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QTextBrowser>
#include <QTextEdit>
class ChatMessageEdit : public QTextEdit {
public:
ChatMessageEdit(QWidget* parent) : QTextEdit(parent) { }
virtual QSize sizeHint() const { return QSize(0, 25); }
};
int main(int argc, char** argv) {
QApplication app(argc, argv);
QWidget* widget = new QWidget;
QVBoxLayout* layout = new QVBoxLayout;
QTextBrowser* log = new QTextBrowser(widget);
layout->addWidget(log, 1);
ChatMessageEdit* editor = new ChatMessageEdit(widget);
editor->setMinimumHeight(editor->sizeHint().height()); // empty
layout->addWidget(editor);
widget->setLayout(layout);
widget->show();
return app.exec();
}
The minimum size for editor is 25px, and so is it's minimal size. But by some strange reason it is created with a size about 100px that is always preferred to my size hint. Everything other is working as expected: expanding (size hint isn't really fixed in my application), shrinking etc. I tried changing size policy, but with abolutely no result.
This was the minumumSizeHint() method. I overloaded it to return sizeHint(), and everything is working as expected now.
You are also overlooking how layouts work. Please read up here on why your sizes are not being respected in a layout.