Translate instructions QT Creator in C ++
-
Good morning.
I do not understand how to write instructions for Qt 5.4 in C ++ if I take as an example the following instructions generated by Qt Creator. Can you give me a hand? (sorry for my english)<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>prova1</class>
<widget class="QMainWindow" name="prova1">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>prova1</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>160</width>
<height>80</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEdit_2"/>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_3"/>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>22</height>
</rect>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui> -
Why do you want to write this code by hand? It is automatically created by Qt Designer and transformed into C++ during compilation (see the ui_*.h files in your build directory if you want to know how that C++ code looks like). It can be written manually, also in pure C++, if there is a need.
-
to understand how you place widgets on the form. I can not write three labels in position 1,1 - 100,30 - 300,80
thanks. -
Static UIs are usually not a good idea. Better use layouts:
QVBoxLayout *layout = new QVBoxLayout(this); QLabel *label1 = new QLabel; QLabel *label2 = new QLabel; QLabel *label3 = new QLabel; layout->addWidget(label1); layout->addWidget(label2); layout->addWidget(label3);
This will give you a more flexible and better-looking UI.
-
I integrated the class prova1 (prova1.cpp) generated by Qt Creator in this way:
#include "prova1.h" #include "ui_prova1.h" #include <QVBoxLayout> #include <QLabel> #include <QLayout> prova1::prova1(QWidget *parent) : QMainWindow(parent), ui(new Ui::prova1) { ui->setupUi(this); int left = 0; int top = 0; int width = 50; int height = 20; QHBoxLayout *hboxlabelRONE = new QHBoxLayout(); hboxlabelRONE->setGeometry(QRect(0,0,100,30)); left = 0; QLabel *LabelNE = new QLabel ("NE"); LabelNE->setGeometry(QRect(left,top,width,height)); left += width; QLabel *LabelRO = new QLabel ("RO"); LabelRO->setGeometry(QRect(left,top,width,height)); hboxlabelRONE->addWidget(LabelNE); hboxlabelRONE->addWidget(LabelRO); QHBoxLayout *hboxlabelPADI = new QHBoxLayout(); hboxlabelPADI->setGeometry(QRect(100,0,80,30)); left += width; QLabel *LabelPA = new QLabel ("PA"); LabelPA->setGeometry(QRect(left,top,width,height)); left += width; QLabel *LabelDI = new QLabel ("DI"); LabelDI->setGeometry(QRect(left,top,width,height)); hboxlabelPADI->addWidget(LabelPA); hboxlabelPADI->addWidget(LabelDI); QHBoxLayout *hboxlabelBAAL = new QHBoxLayout(); hboxlabelBAAL->setGeometry(QRect(180,0,80,30)); left += width; QLabel *LabelBA = new QLabel ("BA"); LabelBA->setGeometry(QRect(left,top,width,height)); left += width; QLabel *LabelAL = new QLabel ("AL"); LabelAL->setGeometry(QRect(left,top,width,height)); hboxlabelBAAL->addWidget(LabelBA); hboxlabelBAAL->addWidget(LabelAL); QWidget *centralWidget = this->centralWidget(); centralWidget->setLayout(hboxlabelRONE); centralWidget->setLayout(hboxlabelPADI); centralWidget->setLayout(hboxlabelBAAL); } prova1::~prova1() { delete ui; }
But I just get a form with the label "NE" in position around (0, 1/2 height form) and the label "RO" in position approximately (1/2 width, 1/2 height).
The labels "PA", "DI", "BA", "AL" does not appear on the form.
I do not know how to make them look all 6 labels on the top left. Help me? Thanks.