Qt pushbuttons are not shown when running script - qt

The interface is shown completely in Qt, but all the buttons are not shown when running the python script.
in Qt:
in script:
Searched online, didn't find a similar issue.
Thanks in advance if anyone can help me on that!
Additional info
The python script:
class Main(QMainWindow):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
uic.loadUi("edytor.ui", self)
self.layout = []
self.rgb_layout = []
self.loadlayout.clicked.connect(self.getfile)
if __name__ == "__main__":
def run_app():
app = QApplication(sys.argv)
mapp = Main()
mapp.show()
sys.exit(app.exec_())
run_app()
The script for "edytor.ui":
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>notepad</class>
<widget class="QWidget" name="notepad">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>659</width>
<height>191</height>
</rect>
</property>
<property name="windowTitle">
<string>Scan</string>
</property>
<widget class="QPushButton" name="loadlayout">
<property name="geometry">
<rect>
<x>50</x>
<y>90</y>
<width>141</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>-1</pointsize>
<weight>50</weight>
<italic>false</italic>
<bold>false</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">background: white;
border-style: outset;
border-width: 0.5px;
border-radius: 5px;
border-color: gray;
font: 14px;</string>
</property>
<property name="text">
<string>Load Layout Image</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="default">
<bool>true</bool>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
The python code doesn't show error.

Related

setFont() Applying Everything except Size Changes

I am trying to apply Class-Based Font changes, and have found this information: Changing font size of all QLabel objects PyQt5
QFont font_label = QFont("MS Shell Dlg 2", 12, QFont::Normal);
QApplication::setFont(font_label, "QLabel");
The above code successfully applies everything except the Font Size, and calling font_label.setPointSize(x); does not change anything.
Project Details:
Using Qt 5.12.11 with CMake and MSVC
Creating .ui files with QT Designer
Besides implementing QDarkStlyeSheet I do nothing relevant with Style Sheets
No other Font alterations appear in my code
I have also discovered odd reproducible examples:
I thought it had to do with my QLabels being RichText, but changing them all to PlainText did not solve anything.
The only QLabel's that resize are default pointSize ones
My layouts do not seems to be imposing anything that would limit their size changes, and the above situations seems to support this.
This problem occurs when trying to change the font size in any direction.
UI File:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MreApp</class>
<widget class="QMainWindow" name="MreApp">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1920</width>
<height>1050</height>
</rect>
</property>
<property name="windowTitle">
<string>MreApp</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QWidget" name="Back">
<widget class="QLabel" name="label_1">
<property name="geometry">
<rect>
<x>490</x>
<y>660</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>8</pointsize>
</font>
</property>
<property name="text">
<string>I start size 8</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>690</x>
<y>80</y>
<width>481</width>
<height>131</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Try Draggin in a new QLabel in QT Designer, it will listen to the code temporarily.</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>660</x>
<y>570</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>I start size 10</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>780</x>
<y>570</y>
<width>95</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>I start size 10</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>1190</x>
<y>570</y>
<width>371</width>
<height>16</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>(The Label Below is a fresh one, dragged in from QT Designer)</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>1270</x>
<y>690</y>
<width>201</width>
<height>61</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<action name="menu_new">
<property name="text">
<string>New</string>
</property>
</action>
<action name="menu_load">
<property name="text">
<string>Load</string>
</property>
</action>
<action name="menu_save">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="menu_saveAs">
<property name="text">
<string>Save As</string>
</property>
</action>
<action name="menu_userManual">
<property name="text">
<string>User Manual</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
You've set a fixed font size for the QLabels in the ui therefore when you change the application's global font size, it won't affect those.
<font>
<pointsize>10</pointsize>
</font>
Changing the application's font size sets a default size for the widgets. If you set the font size of one specific widget, it will overwrite the default one (this is what happened in your case).
If you want to change the font size, set the widget's font, like:
auto lbl = ui->label_2;
auto font = lbl->font();
font.setPointSize(2);
lbl->setFont(font);

How to automatically resize a child element in Qt Designer?

I am making the GUI of my software with QT Designer.
My GUI has some tabs which I successfully managed to auto-resize in full width when the user makes fullscreen the GUI on his computer.
Then inside each tab, I need to insert a Toolbox. But this time, I didn't succeed in auto-resize this toolbox when the user makes the GUI full screen.
I think the best way to explain to you my issue is to have a look at my screenshots:
My GUI in QT designer:
The preview with the issue:
You can see on the preview that the toolbox is keeping the same size when I go for fullscreen.
Solution
You need to add a layout to tab_Run, e.g. by selecting tabWidget and pressing Ctrl+L (on Windows), so your tree would look like this:
Note: In general, you have to set a layout for every widget, the size of whos children you want to be managed automatically by Qt.
Example
As a reference, here is the ui-file I have created:
<?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="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab_Run">
<attribute name="title">
<string>Run</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QToolBox" name="toolBox">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="page">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>758</width>
<height>443</height>
</rect>
</property>
<attribute name="label">
<string>Page 1</string>
</attribute>
</widget>
<widget class="QWidget" name="page_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>758</width>
<height>443</height>
</rect>
</property>
<attribute name="label">
<string>Page 2</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_Campaigns">
<attribute name="title">
<string>Campaigns</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_Settings">
<attribute name="title">
<string>Settings</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_Help">
<attribute name="title">
<string>Help</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Result
Here is the produced result:

Open .ui file from Linux on Windows

My colleague created a C++ Qt Project on Linux and I "transposed" it on Windows. Now, I am a functional GUI but I don't know why, when I open the .ui file with QtDesigner, the file is empty, all widgets disappeared.
Any idea how to get back my widgets or is it only because the file was created on QtDesigner on Linux ?
Here is a part of the ui file opened on Windows with a text editor:
<?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>874</width>
<height>497</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>110</y>
<width>541</width>
<height>331</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>860</width>
<height>700</height>
</size>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="buildMotionTab_3">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<widget class="QFrame" name="centralFrame_3">
<property name="geometry">
<rect>
<x>-10</x>
<y>0</y>
<width>860</width>
<height>851</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>860</width>
<height>851</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>810</width>
<height>851</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QFrame" name="motorFrameP_3">
<property name="geometry">
<rect>
<x>260</x>
<y>10</y>
<width>281</width>
<height>280</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>250</width>
<height>280</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>300</width>
<height>280</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QSpinBox" name="LineDist">
<property name="geometry">
<rect>
<x>190</x>
<y>150</y>
<width>81</width>
<height>32</height>
</rect>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>470</number>
</property>
<property name="singleStep">
<number>1</number>
</property>
<property name="value">
<number>50</number>
</property>
</widget>
[...]
<widget class="QPushButton" name="StandDownButton">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>StandDown</string>
</property>
</widget>
<widget class="QPushButton" name="Homing1Button">
<property name="geometry">
<rect>
<x>120</x>
<y>70</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Homing X</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>874</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Looks like some thing went wrong in transporting the file (I can't say, but could be copy was done in binary mode? or some lines were not expanded in case you copied and pasted the text ..)
The ui xml you shared is broken at line 127 where it shows [...] which is not valid, in place of this it should show closing xml tags for </widget> , I think 4 closing tags </widget> are missing, then it should be ok.

Event Filter for QCustomPlot

I'm having trouble registering for starter an event in a scrollarea with a Qcustomplot for its widget. I tried install an event filter, but it seems that it works in other scroll area, except its own scroll area. Any advice on fixing this / registering or filtering the event in the qcustomplot/scrollarea? attached is my cpp and the ui...
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include <QWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget plotWidget;
QCustomPlot* customPlot = new QCustomPlot;
int x = ui->plotArea_SA->width();
int y = ui->plotArea_SA->height();
for(int i = 0; i < 100; i++)
{
customPlot->plotLayout()->insertRow(i);
QCPAxisRect *ar = new QCPAxisRect(customPlot);
ar->setMinimumSize(x, y);
customPlot->plotLayout()->addElement(i, 0, ar);
}
customPlot->setMinimumWidth(1800);
customPlot->installEventFilter(this);
this->setWindowState(Qt::WindowMaximized);
ui->plotArea_SA->setWidget(customPlot);
qInfo() << ui->plotArea_SA->verticalScrollBar()->value();
ui->plotArea_SA->widget()->installEventFilter(this);
ui->plotArea_SA->viewport()->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject* /*obj*/, QEvent* evt)
{
if (evt->type() == QEvent::Wheel)
{
// ignore the event (this effectively
// makes it "skip" one object)
evt->ignore();
}
// return false to continue event propagation
// for all events
return false;
}
void MainWindow::wheelEvent(QWheelEvent* event)
{
int numDegrees = event->delta() / 8;
qInfo() << numDegrees;
ui->plotArea_SA->verticalScrollBar()->setValue(ui->plotArea_SA->verticalScrollBar()->value() - numDegrees);
qInfo() << "got here";
event->accept();
}
and the UI
<?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>1214</width>
<height>638</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>ArrowCursor</cursorShape>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QScrollArea" name="searchTable_SA">
<property name="maximumSize">
<size>
<width>300</width>
<height>16777215</height>
</size>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>298</width>
<height>279</height>
</rect>
</property>
<zorder>displayTable_SA</zorder>
</widget>
</widget>
</item>
<item row="1" column="0">
<widget class="QScrollArea" name="displayTable_SA">
<property name="maximumSize">
<size>
<width>300</width>
<height>16777215</height>
</size>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents_3">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>298</width>
<height>278</height>
</rect>
</property>
<zorder>searchTable_SA</zorder>
</widget>
</widget>
</item>
<item row="0" column="1" rowspan="2">
<widget class="QScrollArea" name="plotArea_SA">
<property name="minimumSize">
<size>
<width>0</width>
<height>90</height>
</size>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustIgnored</enum>
</property>
<property name="widgetResizable">
<bool>false</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1000</width>
<height>1000</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>2</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>1000</width>
<height>1000</height>
</size>
</property>
<zorder>displayTable_SA</zorder>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1214</width>
<height>21</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/>
<connections/>
</ui>

Minimize size of Qt widget

Following .ui file, created with Qt 5.7 Designer, shows a text box and a spacer:
<?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>310</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="plainText">
<string>abc</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
For example, if the text field has "abc", it should be one line high; if it has two lines (say "abc\nabc"), OR one long line that requires two lines to display, then the text box should be two lines high.
Minimum height is 0. I tried changing the sizing policy of the text box so that the widget is as small as possible, but none do this.
Equivalent Python the code to do this is:
app=QApplication([])
widget = QWidget()
widget.setLayout(QVBoxLayout())
text_widget = QPlainTextEdit('abc')
# text_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.MinimumExpanding)
widget.layout().addWidget(text_widget)
spacer = QSpacerItem(20, 40, vPolicy=QSizePolicy.Expanding)
widget.layout().addSpacerItem(spacer)
widget.show()
app.exec_()

Resources