I'm trying to load a simple .ui file using QUiLoader and I'm getting the following error:
Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document.
I checked that the .ui file exists and printed its contents.
Code:
QApplication a(argc, argv);
MainWindow w;
w.show();
QUiLoader loader;
qDebug()<< QDir::currentPath();
QFile file("customwidget.ui");
qDebug() <<"File open: "<< file.open(QIODevice::ReadOnly| QIODevice::Text );
QWidget *formWidget;
qDebug() << file.readAll();
qDebug() <<"Loader: "<<(formWidget=loader.load(&file,&w));
file.close();
formWidget->show();
return a.exec();
Output:
"/home"
File open: true
"<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>customWidget</class>
<widget class="QWidget" name="customWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>200</width>
<height>200</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>50</x>
<y>60</y>
<width>87</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
"
Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document.
Loader: QObject(0x0)
The customwidget.ui file was generated using the QTDesigner and is placed at /home.
Why is this not working?
you already read the entire file, do a file.reset() before the load or just don't read it first:
QUiLoader loader;
qDebug()<< QDir::currentPath();
QFile file("customwidget.ui");
qDebug() <<"File open: "<< file.open(QIODevice::ReadOnly| QIODevice::Text );
QWidget *formWidget;
qDebug() << file.readAll();
file.reset();//or file.seek(0);
qDebug() <<"Loader: "<<(formWidget=loader.load(&file,&w));
file.close();
formWidget->show();
Related
I succeeded to moved the QStatusBar to a specific location (repositioning), but when I hover I don't see the tips anymore. I tried QStatusBar.show() or .setVisible(True) but doesn't still work. How would you approach this. Thanks
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<widget name="__qt_fake_top_level">
<widget class="QPushButton" name="greetBTN">
<property name="geometry">
<rect>
<x>70</x>
<y>10</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="statusTip">
<string>Greeting people,...</string>
</property>
<property name="text">
<string>greet</string>
</property>
</widget>
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>30</x>
<y>40</y>
<width>241</width>
<height>80</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_status"/>
</widget>
<widget class="QPushButton" name="closeBTN">
<property name="geometry">
<rect>
<x>160</x>
<y>10</y>
<width>75</width>
<height>24</height>
</rect>
</property>
<property name="statusTip">
<string>About to close</string>
</property>
<property name="text">
<string>close</string>
</property>
</widget>
</widget>
<resources/>
</ui>
converted to Python
# -*- coding: utf-8 -*-
from PySide5.QtCore import *
from PySide5.QtGui import *
from PySide5.QtWidgets import *
class Ui_AppMainWindow(object):
def setupUi(self, AppMainWindow):
if not AppMainWindow.objectName():
AppMainWindow.setObjectName(u"AppMainWindow")
AppMainWindow.resize(303, 190)
self.centralwidget = QWidget(AppMainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.greetBTN = QPushButton(self.centralwidget)
self.greetBTN.setObjectName(u"greetBTN")
self.greetBTN.setGeometry(QRect(70, 10, 75, 24))
self.closeBTN = QPushButton(self.centralwidget)
self.closeBTN.setObjectName(u"closeBTN")
self.closeBTN.setGeometry(QRect(160, 10, 75, 24))
self.gridLayoutWidget = QWidget(self.centralwidget)
self.gridLayoutWidget.setObjectName(u"gridLayoutWidget")
self.gridLayoutWidget.setGeometry(QRect(30, 40, 241, 80))
self.gridLayout_status = QGridLayout(self.gridLayoutWidget)
self.gridLayout_status.setObjectName(u"gridLayout_status")
self.gridLayout_status.setContentsMargins(0, 0, 0, 0)
AppMainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QStatusBar(AppMainWindow)
self.statusbar.setObjectName(u"statusbar")
AppMainWindow.setStatusBar(self.statusbar)
self.retranslateUi(AppMainWindow)
QMetaObject.connectSlotsByName(AppMainWindow)
# setupUi
def retranslateUi(self, AppMainWindow):
AppMainWindow.setWindowTitle(QCoreApplication.translate("AppMainWindow", u"MainWindow", None))
#if QT_CONFIG(statustip)
self.greetBTN.setStatusTip(QCoreApplication.translate("AppMainWindow", u"Greeting people,...", None))
#endif // QT_CONFIG(statustip)
self.greetBTN.setText(QCoreApplication.translate("AppMainWindow", u"greet", None))
#if QT_CONFIG(statustip)
self.closeBTN.setStatusTip(QCoreApplication.translate("AppMainWindow", u"About to close", None))
#endif // QT_CONFIG(statustip)
self.closeBTN.setText(QCoreApplication.translate("AppMainWindow", u"close", None))
# retranslateUi
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import *
import sys
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('Xuntitled.ui', self)
self.statusbar.setVisible(True)
self.statusbar.setStyleSheet('Background:red;')
self.statusbar.setParent(self)
#self.statusbar.showMessage('sqddsfdsfd') # works but if I hover, nothing !
self.statusbar.move(50, 25)
self.gridLayout_status.addWidget(self.statusbar, 1, 1)
self.show()
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()
The expected behavior of a status bar is to be shown on the bottom margin of the window, allowing temporary messages and also providing an internal layout for "extra widgets" displayed in it.
If you want to show the status tip somewhere else, then you probably don't need a QStatusBar at all, and a basic QLabel will suffice.
The only requirement is to override the event() function of the window and check for StatusTip events.
In the following example I assumed that you don't really need the default status bar (so you should remove it from Designer and update the generated uic file accordingly if you use that approach).
class Ui(QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('Xuntitled.ui', self)
self.fakeStatusBar = QLabel()
self.gridLayout_status.addWidget(self.fakeStatusBar)
def event(self, event):
if event.type() == event.StatusTip:
self.fakeStatusBar.setText(event.tip())
return True
return super().event(event)
I send the whole code of my small application (it needs ZeroTier-One installed to be run).
QMainWindow centralWidget contains essentially a QTableView (inside a QGroupBox).
My aim is to resize the whole QMainWindow to fit contents snugly.
This code seems to work correctly vertically (almost correctly: I didn't find a way to shrink table beyond a certain limit), but horizontally cuts table roughly in half.
This is what I get:
... and this is what I would like to have:
(getting rid of blank space at the bottom would be even better, but I suspect this would be another, possibly unrelated, question)
I already perused several answers including: Resize window to fit content and Resize QMainWindow to minimal size after content of layout changes
What am I missing?
import json
import os
import sys
import typing
from PyQt6.QtCore import Qt, QAbstractTableModel, pyqtSlot, QModelIndex, QSettings, QTimer
from PyQt6.QtWidgets import QApplication, QMainWindow, QInputDialog, QHeaderView
from PyQt6.uic import loadUi
from urllib3 import PoolManager
class NetworksModel(QAbstractTableModel):
class Column:
def __init__(self, tag, header):
self.tag = tag
self.header = header
def data(self, item, role):
if role == Qt.ItemDataRole.DisplayRole:
return str(item[self.tag])
class CBColumn(Column):
def data(self, item, role):
if role == Qt.ItemDataRole.CheckStateRole:
return Qt.CheckState.Checked if item[self.tag] else Qt.CheckState
class LiColumn(Column):
def data(self, item, role):
if role == Qt.ItemDataRole.DisplayRole:
return '\n'.join(item[self.tag])
columns = [
Column('name', 'Name'),
Column('id', 'NwID'),
Column('status', 'Status'),
Column('type', 'Type'),
LiColumn('assignedAddresses', 'Addresses'),
CBColumn('allowDNS', 'DNS')
]
def __init__(self):
super().__init__()
self._data = []
def data(self, index: QModelIndex, role: int = ...) -> typing.Any:
r = index.row()
c = index.column()
if r < len(self._data):
e: dict = self._data[r]
return self.columns[c].data(e, role)
def rowCount(self, parent: QModelIndex = ...) -> int:
return len(self._data)
def columnCount(self, parent: QModelIndex = ...) -> int:
return len(self.columns)
def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> typing.Any:
if role == Qt.ItemDataRole.DisplayRole:
if orientation == Qt.Orientation.Horizontal:
return self.columns[section].header
def set(self, data):
self.beginResetModel()
self._data = data
self.endResetModel()
def flags(self, index: QModelIndex) -> Qt.ItemFlag:
f = super().flags(index)
# c = index.column()
# i = self.columns[c]
# if i['formatter'] == bool:
# f |= Qt.ItemFlag.ItemIsUserCheckable
return f
class MainWindow(QMainWindow):
def __init__(self, settings):
super().__init__()
loadUi("mainwindow.ui", self)
self.settings: QSettings = settings
self.authtoken = self.settings.value('local/authtoken', None)
self.pm = PoolManager()
self.model = NetworksModel()
self.networks.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
self.networks.verticalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
self.networks.setModel(self.model)
self.refresh_timer = QTimer()
self.refresh_timer.timeout.connect(self.on_actionrefresh_triggered)
if self.authtoken:
self.refresh_timer.start(5*1000)
self.on_actionrefresh_triggered()
# self.main_layout.setSizeConstraint(QLayout.SizeConstraint.SetFixedSize)
#pyqtSlot()
def on_actionauthtoken_triggered(self):
authtoken, ok = QInputDialog.getText(self, 'enter authtoken', '24 alphanumerics')
if ok:
self.authtoken = authtoken
# TODO: enable refreshing
self.settings.setValue('local/authtoken', authtoken)
self.settings.sync()
self.on_actionrefresh_triggered()
self.refresh_timer.start(5*1000)
#pyqtSlot()
def on_actionrefresh_triggered(self):
if self.authtoken:
r = self.pm.request('GET', 'http://localhost:9993/network', headers={'X-ZT1-AUTH': self.authtoken})
if r.status == 200:
j = r.data
li = json.loads(j)
self.model.set(li)
self.trigger_resize()
else:
print(f'on_actionrefresh_triggered() request ERROR: {r.status}')
self.refresh_timer.stop()
else:
print('on_actionrefresh_triggered() ERROR: missing authtoken')
self.refresh_timer.stop()
def trigger_resize(self):
def _resize_me():
self.adjustSize()
# self.resize(self.minimumSizeHint())
QTimer.singleShot(10, _resize_me)
if __name__ == '__main__':
os.environ['DISPLAY'] = "localhost:10.0"
app = QApplication(sys.argv)
window = MainWindow(QSettings('Condarelli', 'ZT1'))
window.show()
# Start the event loop.
exit(app.exec())
and mainwindow.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>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="main_layout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Networks:</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTableView" name="networks">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>20</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionauthtoken"/>
<addaction name="actionrefresh"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionauthtoken">
<property name="text">
<string>authtoken</string>
</property>
<property name="toolTip">
<string>set authtoken</string>
</property>
</action>
<action name="actionrefresh">
<property name="text">
<string>refresh</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
When I have a QCheckBox with tristate set true, it does not repaint itself when transitioning from Qt:PartiallyChecked to setChecked(true) programatically.
A minimal example built in QDesigner places a QCheckBox and QPushButton on the central widget of the default MainWindow. The QCheckBox is default configured except tristate is checked. In code, the ui checkBox is set to Qt::PartiallyChecked in the constructor, and an on_pushButton_clicked() function calls ui->checkBox->setChecked( true );
Expected behavior: when pushButton is clicked, checkBox should change from partially checked to checked appearance.
Observed behavior: checkBox appearance does not change until moused over.
Note: it's even worse when the checkbox has been restyled to be used as a tri-state indicator and is not enabled for user input... the disabled checkbox doesn't repaint on mouseover then.
I discovered this behavior in Ubuntu 18.04 default using the native 5.9.5 Qt libraries running in release mode, but it is also reproducible in 5.14.2 and many other library versions.
The sample code below is 99% the default Qt Widgets application with modification as described above.
The only workaround I have found so far is to repaint the QCheckBox widget anytime its state is changed... not great, but at least it works.
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{ ui->setupUi(this);
ui->checkBox->setCheckState( Qt::PartiallyChecked );
}
MainWindow::~MainWindow()
{ delete ui; }
void MainWindow::on_pushButton_clicked()
{ ui->checkBox->setChecked( true );
// Uncomment the repaint() for a kludgey fix
// ui->checkBox->repaint();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.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>173</width>
<height>98</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QCheckBox" name="checkBox">
<property name="geometry">
<rect>
<x>40</x>
<y>10</y>
<width>92</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>CheckBox</string>
</property>
<property name="tristate">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>40</x>
<y>50</y>
<width>89</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
Tristate.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
Best patch I know so far is to repaint the QCheckBox whenever its state is changed.
I am using QFormLayout but second item of row is not getting vertically wrapped.
.ui file content is
<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>524</width>
<height>281</height>
</rect>
</property>
<layout class="QFormLayout" name="flPatientInfo">
<property name="rowWrapPolicy">
<enum>QFormLayout::WrapLongRows</enum>
</property>
<property name="verticalSpacing">
<number>5</number>
</property>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
Code where row is added -
for (auto info : patientInfo.toStdMap())
{
QLabel *fieldData = new QLabel(GetTranslatedString(info.second.second));
fieldData->setProperty("FieldData", true);
m_GUI->m_UI->flPatientInfo->addRow(GetTranslatedString(info.second.first), fieldData);
}
Actual Result
Expected Result
QFormLayout::RowWrapPolicy has to do how labels and the widgets are layed out, not if the widgets should break. If the space available for label + widget is not sufficient, the widget is simply layed out in a new row when using QFormLayout::WrapLongRows.
You add a QLabel as the widget, which usually will not break text, but write in a single line. You need to enable word wrap on that label using setWordWrap:
for (auto info : patientInfo.toStdMap())
{
QLabel *fieldData = new QLabel(GetTranslatedString(info.second.second));
fieldData->setProperty("FieldData", true);
fieldData->setWordWrap(true); // HERE
m_GUI->m_UI->flPatientInfo->addRow(GetTranslatedString(info.second.first), fieldData);
}
I'm new in Qt and trying to do a text editor example from Qt 5. But, I'm doing that without QtCreator. Of course I have QtCreator installed, I just want to try doing that example without QtCreator. My steps in doing this are:
Write main.cpp, notepad.cpp and notepad.h exactly like in the
example. (Except for include preprocessor, I write the complete
path like:
#include <qt/QtWidgets/QMainWindow>
not just:
#include <QMainWindow>
Create notepad.ui file with QtDesigner.
Generate ui_notepad.h file with uic-qt5 notepad.ui > ui_notepad.h
command.
Generate notepad.pro file with qmake-qt5 -project command.
Add these lines in notepad.pro file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
Generate Makefile with qmake command.
Do make command.
Those step is what I can understand on how QtCreator doing the task automatically. And then, make complaints about incomplete type and forward declaration. But, If I'm doing this with QtCreator, the project is compiled just fine.
What did I miss there?
These are the error messages that I get:
notepad.cpp: In constructor ‘Notepad::Notepad(QWidget*)’:
notepad.cpp:4:72: error: invalid use of incomplete type ‘class Ui::Notepad’
Notepad::Notepad (QWidget* parent) : QMainWindow (parent), ui (new Ui::Notepad) {
^
In file included from notepad.cpp:1:0:
notepad.h:4:8: error: forward declaration of ‘class Ui::Notepad’
class Notepad;
^
notepad.cpp:5:4: error: invalid use of incomplete type ‘class Ui::Notepad’
ui->setupUi (this);
^
In file included from notepad.cpp:1:0:
notepad.h:4:8: error: forward declaration of ‘class Ui::Notepad’
class Notepad;
^
notepad.cpp: In destructor ‘virtual Notepad::~Notepad()’:
notepad.cpp:9:9: warning: possible problem detected in invocation of delete operator: [-Wdelete-incomplete]
delete ui;
^
notepad.cpp:9:9: warning: invalid use of incomplete type ‘class Ui::Notepad’
In file included from notepad.cpp:1:0:
notepad.h:4:8: warning: forward declaration of ‘class Ui::Notepad’
class Notepad;
^
notepad.cpp:9:9: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined
delete ui;
^
Makefile:660: recipe for target 'notepad.o' failed
make: *** [notepad.o] Error 1
Update
ui_notepad.h file:
/********************************************************************************
** Form generated from reading UI file 'notepad.ui'
**
** Created by: Qt User Interface Compiler version 5.4.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_NOTEPAD_H
#define UI_NOTEPAD_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QWidget *centralwidget;
QVBoxLayout *verticalLayout_2;
QVBoxLayout *verticalLayout;
QTextEdit *textEdit;
QPushButton *quitButton;
QMenuBar *menubar;
QStatusBar *statusbar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QStringLiteral("MainWindow"));
MainWindow->resize(800, 600);
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QStringLiteral("centralwidget"));
verticalLayout_2 = new QVBoxLayout(centralwidget);
verticalLayout_2->setObjectName(QStringLiteral("verticalLayout_2"));
verticalLayout = new QVBoxLayout();
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
textEdit = new QTextEdit(centralwidget);
textEdit->setObjectName(QStringLiteral("textEdit"));
verticalLayout->addWidget(textEdit);
quitButton = new QPushButton(centralwidget);
quitButton->setObjectName(QStringLiteral("quitButton"));
verticalLayout->addWidget(quitButton);
verticalLayout_2->addLayout(verticalLayout);
MainWindow->setCentralWidget(centralwidget);
textEdit->raise();
quitButton->raise();
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QStringLiteral("menubar"));
menubar->setGeometry(QRect(0, 0, 800, 27));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QStringLiteral("statusbar"));
MainWindow->setStatusBar(statusbar);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
quitButton->setText(QApplication::translate("MainWindow", "Quit", 0));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_NOTEPAD_H
notepad.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="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="textEdit"/>
</item>
<item>
<widget class="QPushButton" name="quitButton">
<property name="text">
<string>Quit</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
<zorder>textEdit</zorder>
<zorder>quitButton</zorder>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Classes namespaced in Ui:: are automatically generated by the Qt UIC metacompiler after compiling .ui files. Your code expects Ui::Notepad to be generated and automatically linked into the project with qmake/uic.
After looking at the XML of the .ui file that QtDesigner creates, you can see the following line:
<class>MainWindow</class>
That means that the class Ui::MainWindow is generated. If you wanted to generate Ui::Notepad instead, open the .ui form in QtDesigner and rename the top level widget from MainWindow to Notepad. Then Ui::Notepad would appear in your project.
It would cause the XML to look like <class>Notepad</class> which would make Ui::Notepad available in your code