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
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();
}
Below I present a full Qt project in which I was expecting to see a label and a button showing in a scrollarea, but it seems I'm not expressing myself as Qt expects. What Qt expects?
project.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled13
TEMPLATE = app
SOURCES += main.cpp\
widget.cpp
HEADERS += widget.h
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QHBoxLayout>
#include <QScrollArea>
#include <QLabel>
#include <QPushButton>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout *mainLayout = new QHBoxLayout(this);
QScrollArea *scrollArea = new QScrollArea;
mainLayout->addWidget(scrollArea);
QWidget *widget = new QWidget;
scrollArea->setWidget(widget);
QHBoxLayout *hLayout = new QHBoxLayout;
widget->setLayout(hLayout);
QLabel *label = new QLabel("somelable");
hLayout->addWidget(label);
QPushButton *bt = new QPushButton("click");
hLayout->addWidget(bt);
}
Widget::~Widget()
{
}
In this specific code the problem is that you call
scrollArea->setWidget(widget);
before you add layout of the widget. Move the above mentioned call to the end of the constructor and you see the label and the button inside the scrollarea.
It's also mentioned in the QScrollArea::setWidget documentation:
Note that You must add the layout of widget before you call this
function; if you add it later, the widget will not be visible -
regardless of when you show() the scroll area.
probably a simple question: I just created a new project in Qt creator and I set it to use QWidget when I created it, now how do I get rid of the window that it automatically creates when I run it? I also created my own QWidget window which I want to be the only window.
#include "widget.h"
#include <QtGui>
Widget::Widget()
{
QWidget* window = new QWidget;
addBtn = new QPushButton("Add Module");
text = new QTextEdit();
text->setReadOnly(true);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(addBtn,5);
layout->addWidget(text);
window->setLayout(layout);
window->show();
}
Widget::~Widget()
{
}
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
change it like this
Widget::Widget()
{
addBtn = new QPushButton("Add Module");
text = new QTextEdit();
text->setReadOnly(true);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(addBtn,5);
layout->addWidget(text);
this->setLayout(layout);
}
and try to have some time on look and try some of Qt Example (you can find it in Qt Creator)
and there is about 100 short video to learn quickly basic stuff on Qt
Qt is fun, enjoy it.
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();
}
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();
}