I have two applications:
Both displays QCheckBox text correctly
Both use the same css stylesheet
All the settings in the widget editor seem to the the same
The check box square is not visible for one application while it is for the other.
Editor view of the QCheckbox.
The application that is working
The application that is not working.
In response to Pavel Strakhov:
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QCheckBox" name="c">
<property name="text">
<string>Display</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="logToFile">
<property name="text">
<string>To File</string>
</property>
</widget>
</item>
</layout>
This issue is cause because there is a global image file used, so the check box will not be visible. I resolve the issue by making a new checkbox image and applied it to all checkboxes in the css.
Related
I am building a QWidget with QRadioButtons at different levels. In other words, my widget contains some radio buttons and a subwidget (labeled groupBox in the screenshot) that also contains radio buttons.
Here is my problem: the radio buttons inside groupBox seem to interfere with the top level radio buttons (radioButton_1 and radioButton_2). I would expect that exactly one of radioButton_1 and radioButton_2 is checked at any given time, but it is now possible to uncheck these by clicking on the currently checked radio button.
The fix I came up with is to add setChecked(true) to the signal handler for radioButton_1.clicked() and radioButton_2.clicked(), but this seems a bit hacky.
connect(ui->radioButton_1, &RadioButton::clicked, [this]() {
ui->radioButton_1.setChecked(true);
});
connect(ui->radioButton_2, &RadioButton::clicked, [this]() {
ui->radioButton_2.setChecked(true);
});
Is there a better way to get the functionality back? Perhaps a function like setRadioButtonGroup({ui->radioButton_1, ui->radioButton_2}).
EDIT:
Per request for a MCVE, below is the form mainwindow.ui. Other files (mainwindow.cpp, main.cpp, mainwindow.h) are just the boilerplate provided when a QWidget Application is created in Qt Creator.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QRadioButton" name="radioButton_1">
<property name="text">
<string>radioButton_1</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_2">
<property name="text">
<string>radioButton_2</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>groupBox</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QRadioButton" name="radioButton_3">
<property name="text">
<string>radioButton_3</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_4">
<property name="text">
<string>radioButton_4</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
To address this, I suggest you create a QButtonGroup and add all four radio button to it.
See https://doc.qt.io/qt-5/qbuttongroup.html
I use QGridLayout very often, and there's a requirement I don't know how to or if I can achieve with this kind of layouts.
My question: Imagine I have two normal widgets (derived from QWidget) on the left and right (on something like QHBoxLayout or QGridLayout), and I would like to have the line separating them movable by the user. Is that possible?
More information:
To give an example, imagine the default Windows registry editor. You have the part on the left, where there are keys and paths, and on the right, where there are values to be edited.
I would like to emphasize that I'm not asking for an explorer view. What I have basically is a plot widget on the right, and a QTableView widget on the left, and I would like the user to be able to conveniently scale with his mouse, which widget should be horizontally bigger.
Is there some kind of Layout that is scalable by mouse?
Please ask for more information of you require it.
I think you should use a QSplitter.
According to the documentation:
A splitter lets the user control the size of child widgets by dragging
the boundary between the children. Any number of widgets may be
controlled by a single splitter.
For example, using Qt Creator, if we have two QGridLayout with a QPushButton on each one, we can select both QGridLayout and use the Lay Out Horizontally in Splitter option.
After that, we could move the boundary between them to control the size of child widgets:
I made an example. Here you have the code for the ui file:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>435</width>
<height>105</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QSplitter" name="splitter">
<property name="geometry">
<rect>
<x>9</x>
<y>10</y>
<width>411</width>
<height>71</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QWidget" name="gridLayoutWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="gridLayoutWidget_2">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
I have a bunch of standard qt creator dragged-on controls, which I have at the bottom of a window. I have one table widget that I want to expand vertically to be able to display more rows if the window is expanded downwards, less if upwards to a minimum. I want all the other widgets to stick to the bottom of the window as that edge moves. I'm using Qt Creator 2.1.0 and Qt 4.7, and I don't see any flags for the widgets that would do what I need, nor did googling really turn up anything enlightening. Perhaps it has something to do with putting the widgets in a container that moves and/or stretches? Right now, they're just on MainWindow.
Just set a vertical layout, add a spacer to it, and then your widget. The spacer will push your widget all the way down. Additionally you might want to toy around with the size policies.
A QMainWindow should only be used if you need its docking functionality. Otherwise, just use a plain QWidget. No general purpose child widgets can be "stuck" onto a QMainWindow. They need to belong to the central widget. All you most likely need to do is to set an Expanding vertical size policy to the table widget. Designer should do it by default. Here's an example:
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableWidget" name="table">
<row>
<property name="text">
<string>1</string>
</property>
</row>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
<item row="0" column="0">
<property name="text">
<string>fyngyrz</string>
</property>
</item>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="foo">
<property name="text">
<string>Foo</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="bar">
<property name="text">
<string>Bar</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
It is difficult to know what you are trying to do from your question, but perhaps you are looking for QSplitter.
Alternatively, you can specify various QLayouts for a QWidget, such as QHBoxLayout, QVBoxLayout, QGridLayout, etc. Various widgets can be added to the layout you specify, then you set the layout on a QWidget, so it acts as a container for the widgets you've added to the layout. Stretching and alignment flags can be set as you add your widgets to the layout.
new to QT and I am planning to display images aligning beautifully with layouts.
I create 3 Qlabels and group them in vertical and horizontal layouts to construct a widget like:
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
in designer:
I use following to load my images and display images:
ui->setupUi(this);
src=cv::imread("image2.bmp");
cvtColor(src,src,CV_BGR2RGB);
img = QImage((const unsigned char*)(src.data),src.cols,src.rows,src.cols*src.channels(),QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(img).scaled(ui->label->width(), ui->label->height(),Qt::KeepAspectRatio));
ui->label_2->setPixmap(QPixmap::fromImage(img).scaled(ui->label_2->width(), ui->label_2->height(),Qt::KeepAspectRatio));
ui->label_3->setPixmap(QPixmap::fromImage(img).scaled(ui->label_3->width(), ui->label_3->height(),Qt::KeepAspectRatio));
displaying image spans 480*640, and I choose to keep the aspect ratio.
however, the program runs like this:
pic lays in screen shots here
the image is resizing in a surprisingly wrong way. I have try Qt::KeepAspectRatiobyExpanding and Qt::ignoreAspectRatio but none of these behavior as expect.
Any idea about this?
You can set the image to fill the label with: -
void QLabel::setScaledContents(bool)
So call: -
ui->label->setScaledContents(true);
ui->label_2->setScaledContents(true);
ui->label_3->setScaledContents(true);
Also note that you can set the pixmap in the designer. Just add the images as a resource, then set it on the label, in its properties: -
I cannot see the images you've pasted, but perhaps the problem is based on the size of QLabels: what is their size prior to:
ui->label->setPixmap(QPixmap::fromImage(img).scaled(ui->label->width(), ui->label->height(),Qt::KeepAspectRatio));
Perhaps it's very small or just enough to hold the "TextLabel" string, hence the weird resizing?
I've designed a ui in QtDesigner which has a QLabel that must show a JPG image (included in a Qresource file) as pixmap.
The Application works fine in KDE (not tested under gnome), but in windows the image is not shown.
here is the XML code in the ui file which impeliments the QLabel file:
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="lib.qrc">:/image/Yearbook_1970.jpg</pixmap>
</property>
</widget>
</item>