Add DockWidget on Main Window - c++

This is my UI
I hope I could move red border to left or right in the picture by mouse when the program run.
How should I do?

Add all those widgets to the form in the UI designer. Select text edit widget and list widget. Select "Lay Out Horizontally in Splitter" from the top toolbar. Then select the form itself and select "Lay Out Vertically" from the top toolbar. That's it, except the list widget is not dockable.
Edit:
If you want to keep the list widget at constant size (so that it is resized only by the user), tell the QSplitter the stretch factors of the widgets, for example like this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// index 0 is the text edit, stretch factor is 1 (= all stretching goes here)
ui->splitter->setStretchFactor(0, 1);
// index 1 is the list widget, stretch factor is 0
ui->splitter->setStretchFactor(1, 0);
}

Related

How to determine current border color of QGroupBox?

I am using Qt 6.3 on Windows 11, and I am trying to figure out the current border color of QGroupBox in Fusion style.
I have subclassed QProxyStyle as my class GuiStyle, in my MainWindow constructor:
ui.setupUi(this);
auto guiStyle = new GuiStyle;
guiStyle->setBaseStyle(QStyleFactory::create("Fusion"));
qApp->setStyle(guiStyle);
qApp->setPalette(QPalette{QColor{0,0,0}});
My groupbox looks like this:
To figure out the current actual color of the border of QGroupBox I use QColorDialog and use the Pick Screen Color option then place my cursor exactly on top of the border of the QGroupBox. For different color of QPalette applied to QApplication I get different results:
qApp->setPalette(QPalette{QColor{0,0,0}}) => QColor(ARGB 1, 0.0901961, 0.0901961, 0.0901961)
qApp->setPalette(QPalette{QColor{10,10,10}}) => QColor(ARGB 1, 0.121569, 0.121569, 0.121569)
qApp->setPalette(QPalette{QColor{20,20,20}}) => QColor(ARGB 1, 0.152941, 0.152941, 0.152941)
Clearly, the border color of the QGroupBox is palette dependent. When I print out all colors of the current QPalette for each QPalette::ColorGroup and QPalette::ColorRole none of the color seems to match the current color which I get from the color picker of the QColorDialog. I tried to get all the values of the palette in the void GuiStyle::drawComplexControl function when the complex control is CC_GroupBox, in the void GuiStyle::drawPrimitive function when the primitive element is PE_Frame or PE_FrameGroupBox. But none of the colors seems to match the current border color of the QGroupBox. Is there any way I could determine this color programmatically?

Is there a way to reassign a grid layout to a label?

I have created a GUI with the aid of Tkinter that has created a 3 [C]olomn by 8 [R]ow, (I have placed the borders of the 'cells' in red) and output is like so:
Just to calrify:
Where the "logo [E]" is (C1,R1) & (C1,R2) are rowspanned and where the "main window" is from (C2,R3) to (C3,R8) are colomnspanned and rowspanned.
All are Label widget except for (C1,R3 to C1,R8), which are button widgets with a command to display a string in "Main Window" (gray area).
My aim is to make the "Main Window"(gray area) display only in different grid layouts based on the selected button on the left. So for example I would like to make the grey have a grid layout of 2col by 4 row when C1,R6 is pressed, or when C1,R3 is pressed the grid layout to be 2Cx6R all in the gray area.
Q: Is there a way of re-gridding a label? If so how? Or an alternative approach?
ADDENDUM:
Ideally I would like to update the GREEN area with different number of row and column configurations. For example: When Micro Audit is touched, the Green area will configure itself to have 4 rows by 2 columns, and when Find Item is selected, it will only 3 rows with 1 column, etc. :

QT gui items won't shrink to ft to their parent widget

I have three widgets under my main window . I design my program that each widget will be controlled by a class .
Last 2 hours I waste my time to fit 3 GUI elements to one of this widgets.
I want to have a label and a line item and a button. I put them in to a grid layout(in real case I have 3 row for simplicity I put 1 row in the example below). And set the grid layout's parent to my class.
What I expect is this GUI items to resize themselves and fit to one row in this widget, no matter what I tried I couldn't succeed .
In short I expect this items to shrink to fit to the widget by themselves. But couldn't figure out how to do it. Any advice ?
void
enviromentSetup::createDialogs()
{
numberOfPoints = new QLabel (QApplication::translate("leftPanel","numberOfPoints"));
inputNumberOfPoints = new
QLineEdit(QString::number(st_environmentParamaters.number_of_points_in_line));
maxElementsButton = new QPushButton("Max Elems");
gridLayout = new QGridLayout(this);
gridLayout->addWidget(numberOfPoints, 0, 0);
gridLayout->addWidget(inputNumberOfPoints, 0, 1);
gridLayout->addWidget(maxElementsButton, 0, 2);
this->show();
}
It is insufficient to merely make the parent of the layout the main widget. You also have to explicitly set the layout of the widget.
Add the following line just before you show the widget:
this->setLayout(gridLayout);

How can I create a 3 state custom push button?

I need to create a custom push button which will have 3 different background images corresponding to the following states:
normal
mouse over
mouse down
I would like to have a QHBoxLayout with 3 parts for left side, right side and middle side (stretching side) for the button.
Inside the middle size, I would like to have a label to show the text.
I need this button to have a "clicked" event as well.
I have been doing a lot of search to achieve this but I am really lost. I tried many things including custom widget from QWidget or styling QPushButton with stylesheets but I failed to achieve having 3 images for 3 mouse states and the clicked event.
I am looking for help.
You can use the border-image property, by composing a single image for each state like this:
| |
Left Image | Middle Image | Right Image
| |
then by specifying the left and right image sizes as the border slice sizes and in the border widths of the stylesheet. For example, if the right images were 25 pixel wide and the left ones 20, you would have:
QPushButton {
border-image: url(:/normal.png) 0 25 0 20 stretch stretch;
border-width:0 25 0 20; /* without it, only the middle images would show */
}
QPushButton:hover {
border-image: url(:/hover.png) 0 25 0 20 stretch stretch;
}
QPushButton:pressed {
border-image: url(:/pressed.png) 0 25 0 20 stretch stretch;
}
The values represent the distance between the top, right, bottom and left sides of the image.
well,first,you should have 3 picture for 3 states,and then you can use function setStyleSheet .
QPushButton *btn = new QPushButton;
btn->setStyleSheet("QPushButton{background:url(:/Resources/pause_nor.png);border:0px;}"
"QPushButton:hover{background:url(:/Resources/pause_over.png);border:0px}"
"QPushButton:pressed{background:url(:/Resources/pause_over.png); position: relative;top: 1px; left: 1px;}");

How do I create an image comparator in qt?

I have two images--img1 and img2--and I would like to be able to compare both images. I would like to overlay them over each other with a slider which would let me see more of img1 or img2. Lastly, when I move in one image I would also like to move in the other img. This is what I have so far.
QGraphicsScene *scn = new QGraphicsScene( this );
ui->view->setScene( scn );
QPixmap *im = new QPixmap("P3C.jpg");
QPixmap *i = new QPixmap("result.jpg");
scn->addPixmap( *im );
scn->addPixmap(*i);
Use QGraphicsItemGroup to group the 2 pixmap items so they will act as one. Set the item group to ItemIsMovable so you can move them.
Set the opacity of the item that is on top of the other so the bottom one can show thru. You can connect the value change signal of your slider widget to your object's slot to control the opacity.