Why am I unable to delete tabWidget in Qt Designer? - c++

I am using Qt Designer in combination with MSVC2019. I recently changed my form's base class from QWidget to QMainWindow manually by editing the .ui file, header, and constructor. In this form I have a tabWidget, which is now not able to be selected. It shows up in the object inspector and property editor, and I can edit its properties, but not select it. This is unlike all the other widgets in my form. When I right-click on the tabWidget in the Object inspector it gives the same menu options as a QMainWindow (Create Menu Bar, Add Tool Bar).
I have tried changing various object properties of the tabWidget and its main window but nothing has worked.
Here is the .ui region of interest. All I did was replace Widget with QMainWindow in line 4.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>campanel</class>
<widget class="QMainWindow" name="campanel">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1509</width>
<height>984</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>FASTPLOTTER</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>bigmoves.ico</normaloff>bigmoves.ico</iconset>
</property>
<property name="windowOpacity">
<double>1.000000000000000</double>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="dockNestingEnabled">
<bool>true</bool>
</property>
<property name="dockOptions">
<set>QMainWindow::AllowNestedDocks|QMainWindow::AllowTabbedDocks|QMainWindow::AnimatedDocks|QMainWindow::ForceTabbedDocks</set>
</property>
<widget class="QTabWidget" name="tabWidget">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="tabPosition">
<enum>QTabWidget::West</enum>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<property name="documentMode">
<bool>true</bool>
</property>
<property name="tabsClosable">
<bool>true</bool>
</property>
<property name="movable">
<bool>true</bool>
</property>
<widget class="QWidget" name="iolji">
<attribute name="title">
<string/>
</attribute>
</widget>
<widget class="QWidget" name="tab">
<attribute name="title">
<string/>
</attribute>
</widget>
</widget>

QMainWindow is a special case of QWidget: it has a menu bar, a status bar, tool bars, can add dockable frames, and have a central-widget. While you can switch between QWidget and QDialog in UI files, when switching to QMainWindow you must embed all the content of previous widget into the centralWidget:
<widget class="QMainWindow" name="MainWindow">
<!-- size policy, geometry... -->
<widget class="QWidget" name="centralwidget">
<!-- size policy, layout, content of your previous QWidget -->
</widget>
</widget>

Related

how to center a middle widget and expand its cell

In Qt designer, how do i center a middle widget in a vertical/horizontal/grid layout and expand its cell at the same time, what i want to get is something like this:
what i get:
One possible solution is to place the middle QPushButton in a QWidget through a layout, and then place that QWidget in the second column of the QHBoxLayout:
<?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>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_3">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Output:
You're gonna need to add a horizontal spacer. And then for the spacer you can add properties. Whether it should be fixed or expanding.
So first select and add a horizontal spacer, and then when you select the horizontal spacer, in the properties widget you'll be able to set properties to it:
The way the spacer will be placed in-between two widgets, is by first dragging the spacer and placing it in between the widgets, selecting all of them and then right-click and group horizontally. This will put your widgets and the spacer in a horizontal layout:
If you want to do it in the code manually, look into QSpacerItem.

QPushButton changes size of widgets in layout

This problem is driving me insane and I can't seem to find a logical answer. I'm pretty new to this so please bear with.
I've been creating an application where I create a vertical 'navigation bar' and need to add QPushButtons dynamically. I've noticed that the
horizontal position of a label in a QVBox changes when a QPushButton is added to it.
I've created a minimal version:
Before Adding QPushButton:
Label reaches the edge of the application
After adding QPushButton:
Label width is reduced slightly
Here is the code I'm using to dynamically add the QPushButton:
void MainWindow::on_pushButton_clicked()
{
QPushButton *newButton = new QPushButton("Test");
newButton->setContentsMargins(0,0,0,0);
newButton->setStyleSheet("margin: 0; padding: 0;");
ui->verticalLayout->setMargin(0);
ui->verticalLayout->setContentsMargins(0,0,0,0);
// add new push button inside VBox
ui->verticalLayout->addWidget(newButton);
}
.ui file:
<?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>203</width>
<height>224</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>201</width>
<height>151</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="styleSheet">
<string notr="true">border: 1px solid white;</string>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>10</x>
<y>180</y>
<width>181</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Add Push Button To VBox</string>
</property>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
As you can see setting the margins on both the QPushButton and the layout don't seem to have any effect. Is there anyone who could shed some light on this issue?
Let's analyze your .ui with the help of Qt Designer, if we stretch the window you get the following:
As you can see the layout only affects the QLabel, so the initial button will not be handled by the layout, but the added button will be. Therefore, you do not observe a similar behavior.
The solution is to restructure the design using the following structure:
QMainWindow
└── QVBoxLayout
├── QLabel
└── QWidget
└── QVBoxLayout
└── QPushButton
You must change the size policy of the QWidget to take the minimum height by setting Maximum in Vertical Policy:
And in that second layout add the button. The .ui is the following:
<?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>335</width>
<height>303</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="styleSheet">
<string notr="true">border: 1px solid white;</string>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Add Push Button To VBox</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Then it is no longer necessary to modify the button:
void MainWindow::on_pushButton_clicked()
{
QPushButton *newButton = new QPushButton("Test");
ui->verticalLayout->addWidget(newButton);
}
Obtaining the following:

QT multiple forms, dynamic layout

I am doing my first larger project using the QT framework and as my menu begins to scale more I feel like I need either the ability to swap out multiple central widgets, or have a new form all together but without opening another window. I cannot seem to figure out how to go about this.
Essentially I would want the user to click say the "settings" button, and for all the widgets to either hide themselves, and for the new ones to show themselves, without opening another window and sticking to just 1 window. Here is the cat output to my .ui file
<?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>800</width>
<height>600</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>520</y>
<width>781</width>
<height>71</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="joinGamePushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Join Game</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="settingsPushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Settings</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="aboutPushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>About DMUX</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="exitPushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Exit</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections>
<connection>
<sender>exitPushButton</sender>
<signal>clicked()</signal>
<receiver>MainWindow</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>692</x>
<y>555</y>
</hint>
<hint type="destinationlabel">
<x>399</x>
<y>299</y>
</hint>
</hints>
</connection>
</connections>
</ui>
What exactly is the best approach here? Should I use several central widgets and somehow drop them in as needed, or use multiple formed and load them when a button is clicked? I am using QT Creator for this project at the moment, so it would be preferred that I use it's generation of code instead of writing a solution myself.
I am doing my first larger project using the QT framework and as my
menu begins to scale more I feel like I need either the ability to
swap out multiple central widgets, or have a new form all together but
without opening another window. I cannot seem to figure out how to go
about this.
Either QMainWindow::setCentralWidget for inserting new widget as 'central' or make a permanent 'central' QStackedWidget for just flipping between stacked widgets.
Which one is better? Both would require to refactor your template and separate widgets you would like to embed depending on user selection as separate .ui pieces or maybe just fully programmatically created ones.
setCentralWidget would need you to take care of disposing previous widget when inserting new one (simpler) or using some type of container to keep already created 'detached' widgets in there (more complex: prone to bugs).
QStackedWidget as selector-viewport would need you to precreate widgets or possibly create and stack new ones on demand.

Setting QScrollArea as central widget in Qt

In Qt I have removed the central widget in an xml editor and replaced it with a QScrollArea, this works when I preview it in QtDesigner:
but when I run the program it looks like this:
Are you not meant to remove the central widget or is there a sizePolicy I have to change?
Here is the ui file:
<?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>179</width>
<height>191</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>224</width>
<height>628</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>75</pointsize>
</font>
</property>
<property name="midLineWidth">
<number>2</number>
</property>
<property name="text">
<string>label
</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>179</width>
<height>19</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="1.qrc"/>
</resources>
<connections/>
</ui>
Check your ui_mainwindow.h. See if there is a line like:
MainWindow->setCentralWidget(scrollArea);
You can use QMainWindow::setCentralWidget function in your mainwindow.cpp to set a central widget to your main window:
setCentralWidget(myScrollArea);
I don't think there is any way of changing the central widget from the designer.
I am getting proper window when I ran the program with your ui file content. I don't know why it is not creating problem like you have.
I would recommend you to use Qt designer inbuilt in Qt Creator which I felt easier to use.
Also, I think the problem can be one of these:
Generally central widget is good as it provides a base on which you can place all your layouts. So, it is good to have it in your UI.
Here, take care that after inserting widgets into layouts you don't simply break those layouts. The designer will reset the widget width and height. So you have to set them again.
Next, this may have occured if the ui file loaded in designer is not up-to-date while it is changed on disk.
Keep track of all the layouts, the geometry and sizePolicy of widgets.
I won't be able to answer in code since when I used the ui file content which you have given, it doesn't cause problem here.
Create a layout on your mainwindow in which you would put your QScrollArea. This would expand it according to the layout.
P.S. : I open it in Qt Creator.
So please give some more info whether it is fixed when you set the geometry of scrollarea manually.

How to disable word wrapping on QWebView?

I'm using what I believe is a pretty standard example of a WYSIWYG editor from graphics-dojo built using a QWebView. The source can be found here
I'm trying to disable the word wrapping in the QWebView so if an element extends beyond the width of the window, a horizontal scrollbar will appear and no wrapping will occur. By default it appears that the text in the QWebView wraps at all window widths except when the width is resized to under about 100 pixels which is when the horizontal scrollbar appears. Given this behavior I know the horizontal scrollbar is enabled. I've looked at QWebView, QWebFrame, and QWebPage and can't seem to find any references to word wrapping. I wonder if it is a property of the underlying WebKit.
In summary: How can I disable word wrapping on a QWebView so that a horizontal scrollbar appearing is the default behavior?
The QWebView is contained in the htmleditor.ui file like this.
<?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>659</width>
<height>398</height>
</rect>
</property>
<property name="windowTitle">
<string>HTML Editor</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::South</enum>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<property name="documentMode">
<bool>true</bool>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QWebView" name="webView">
<property name="url">
<url>
<string>about:blank</string>
</url>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
...