I'm fairly new to qcustomplot and qt (switching from pyqt+matplotlib for performance upgrade / learning). I've been looking into the tutorial for qcustomplot but all revolves around drawing a UI with a Qwidget being promoted to QCustomPlot, which is great but Im guessing would require manual drawing if I want more than one Qcustomplot / subplots with fixed / view-able size. So far, I've been trying to create a customplot with multiple axis to see if the axis changes size within the scrollarea, but so far no luck. Any help would be appreciated.
The header files
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
The cpp follows:
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include <QWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget plotWidget;
QVBoxLayout layout;
QCustomPlot customPlot;
for(int i = 0; i < 10; i++)
{
customPlot.plotLayout()->insertRow(i);
QCPAxisRect *ar = new QCPAxisRect(&customPlot);
customPlot.plotLayout()->addElement(i, 0, ar);
}
/*
layout.addWidget(customPLot);
plotWidget.setLayout(layout);
*/
ui->plotArea_SA->setWidget(customPlot);
}
MainWindow::~MainWindow()
{
delete ui;
}
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>1269</width>
<height>454</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QScrollArea" name="plotArea_SA">
<property name="geometry">
<rect>
<x>169</x>
<y>9</y>
<width>1091</width>
<height>381</height>
</rect>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</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>1089</width>
<height>379</height>
</rect>
</property>
</widget>
</widget>
<widget class="QScrollArea" name="searchTable_SA">
<property name="geometry">
<rect>
<x>0</x>
<y>10</y>
<width>161</width>
<height>151</height>
</rect>
</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>159</width>
<height>149</height>
</rect>
</property>
</widget>
</widget>
<widget class="QScrollArea" name="displayTable_SA">
<property name="geometry">
<rect>
<x>0</x>
<y>170</y>
<width>161</width>
<height>221</height>
</rect>
</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>159</width>
<height>219</height>
</rect>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1269</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>
The setWidget() function requires as a parameter the pointer of an object that inherits from QWidget. I recommend you create a pointer.
You must change to:
QCustomPlot *customPlot = new QCustomPlot;
for(int i = 0; i < 10; i++)
{
customPlot->plotLayout()->insertRow(i);
QCPAxisRect *ar = new QCPAxisRect(customPlot);
customPlot->plotLayout()->addElement(i, 0, ar);
}
ui->plotArea_SA->setWidget(customPlot);
Related
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>
I have a MainWindow class and within it a QStackWidget.
Inside it I have another 3 widgets, each one is my own custom class derived from QWidget.
Inside one of the widgets I have a button.
When I am using QT Creator and press Go to slot.. on the button, it creates an on_button_clicked event in MainWindow class.
How do I change it to have the event inside my custom class?
UPDATE:
I can see that the buttons are located in MainWindow. This is auto generated, so I am not sure how to move it.
Code:
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_2_clicked()
{
QMessageBox::information(this, "Button 2 clicked!", "Click");
}
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>668</width>
<height>630</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QStackedWidget" name="stackedWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>531</width>
<height>391</height>
</rect>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="Stack1" name="page">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>140</x>
<y>110</y>
<width>321</width>
<height>121</height>
</rect>
</property>
<property name="text">
<string>Button on Stack1</string>
</property>
</widget>
</widget>
<widget class="Stack2" name="page_2">
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>110</x>
<y>140</y>
<width>371</width>
<height>161</height>
</rect>
</property>
<property name="text">
<string>Button on stack2</string>
</property>
</widget>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>668</width>
<height>25</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"/>
<customwidgets>
<customwidget>
<class>Stack1</class>
<extends>QWidget</extends>
<header>stack1.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>Stack2</class>
<extends>QWidget</extends>
<header>stack2.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
I want the button callback to be inside my custom class stack2.cpp
#include "stack2.h"
Stack2::Stack2(QWidget *parent) : QWidget(parent)
{
}
Stack2::~Stack2()
{
}
You made promotion correctly but you should not fill contents of promoted widgets in main window form (i.e. put buttons in them). Instead, create stack page classes as Designer Form Classes (just as MainWindow but based on QWidget) and put contents in them using separate form editor. Alternatively, you can add layouts and buttons to stack pages manually in their constructors.
My setup is as follows:
main.cpp
projectQt.cpp
projectQt.h
tab1.h
tab1.cpp
tab2.h
tab2.cpp
projectQt.ui
ui_projectQt.h
I want to create a project with Ui file. In the Ui file, i have three comboBox and two Apply button and in the tab1.cpp and tab2.cpp, I want to add initial Items of the comboboxes. If the user clicks one of the Apply buttons, run the "OnBtnApplyClicked" methods.
However I get this error:
Error is
projectQt.obj : error LNK2019: unresolved external symbol "public: __thiscall Ctab1::~Ctab1(void)" (??1Ctab1##QAE#XZ) referenced in function "public: __thiscall ProjectQt::ProjectQt(class QWidget *)" (??0ProjectQt##QAE#PAVQWidget###Z)
C:\output\Deneme\Qt\Win32\Debug\\projectQt.exe : fatal error LNK1120: 1 unresolved externals
main.cpp
#include "projectQt.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ProjectQt w;
w.show();
return a.exec();
}
projectQt.ui file:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ProjectQtClass</class>
<widget class="QMainWindow" name="ProjectQtClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>900</width>
<height>900</height>
</rect>
</property>
<property name="windowTitle">
<string>ProjectQt</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>800</width>
<height>800</height>
</rect>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="IDC_FORM_TAB_1">
<attribute name="title">
<string>M001</string>
</attribute>
<widget class="QLabel" name="IDC_LBL_1_0">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>124</width>
<height>13</height>
</rect>
</property>
<property name="text">
<string>COMBO1</string>
</property>
</widget>
<widget class="QComboBox" name="IDC_CMB_1_0">
<property name="geometry">
<rect>
<x>120</x>
<y>40</y>
<width>69</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QComboBox" name="IDC_CMB_1_1">
<property name="geometry">
<rect>
<x>120</x>
<y>90</y>
<width>69</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="IDC_BTN_1_Apply">
<property name="geometry">
<rect>
<x>180</x>
<y>174</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Apply</string>
</property>
</widget>
<widget class="QLabel" name="IDC_LBL_1_1">
<property name="geometry">
<rect>
<x>10</x>
<y>90</y>
<width>124</width>
<height>13</height>
</rect>
</property>
<property name="text">
<string>COMBO2</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="IDC_FORM_TAB_2">
<attribute name="title">
<string>M002</string>
</attribute>
<widget class="QLabel" name="IDC_LBL_2_0">
<property name="geometry">
<rect>
<x>0</x>
<y>30</y>
<width>104</width>
<height>13</height>
</rect>
</property>
<property name="text">
<string>COMBO3</string>
</property>
</widget>
<widget class="QComboBox" name="IDC_CMB_2_0">
<property name="geometry">
<rect>
<x>100</x>
<y>30</y>
<width>69</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="IDC_BTN_2_Apply">
<property name="geometry">
<rect>
<x>100</x>
<y>70</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Apply</string>
</property>
</widget>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>900</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>
<include location="projectQt.qrc"/>
</resources>
<connections/>
</ui>
projectQt.cpp file:
#include "projectQt.h"
#include "tab1.h"
#include "tab2.h"
#include "ui_projectQt.h"
ProjectQt::ProjectQt(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
Ctab1 ctab11;
Ctab1 ctab22;
ctab11.initGUI();
ctab22.initGUI();
connect(ui.IDC_BTN_1_Apply,SIGNAL(clicked()),this,SLOT(Ctab1::OnBtnApplyClicked()));
connect(ui.IDC_BTN_2_Apply,SIGNAL(clicked()),this,SLOT(Ctab2::OnBtnApplyClicked()));
}
ProjectQt::~ProjectQt()
{
}
projectQt.h file:
#ifndef projectQt_H
#define projectQt_H
#include <QtWidgets/QMainWindow>
#include "ui_projectQt.h"
#include "tab1.h"
#include "tab2.h"
class ProjectQt : public QMainWindow
{
Q_OBJECT
public:
ProjectQt(QWidget *parent = 0);
~ProjectQt();
//private:
Ui::ProjectQtClass ui;
};
#endif // projectQt_H
tab1.h file:
#pragma once
#include <string>
#include "projectQt.h"
#include "ui_projectQt.h"
class Ctab1
{
public:
Ctab1(void);
~Ctab1(void);
public slots:
void setEvents();
void initGUI();
void OnBtnApplyClicked();
//private:
//Ui::ProjectQtClass *ui;
};
tab1.cpp file:
#include "projectQt.h"
#include "tab1.h"
#include <QTextStream>
#include "ui_projectQt.h"
Ctab1::Ctab1(void)
{
}
void Ctab1::initGUI(){
Ui::ProjectQtClass uimain;
uimain.IDC_CMB_1_0->addItem("1100");
uimain.IDC_CMB_1_0->addItem("1101");
uimain.IDC_CMB_1_1->addItem("1200");
uimain.IDC_CMB_1_1->addItem("1201");
}
void Ctab1::OnBtnApplyClicked(){
}
tab2.h file:
#pragma once
#include <string>
#include "projectQt.h"
#include "ui_projectQt.h"
class Ctab2
{
public:
Ctab2(void);
~Ctab2(void);
public slots:
void setEvents();
void initGUI();
void OnBtnApplyClicked();
//private:
//Ui::ProjectQtClass *ui;
};
tab2.cpp file:
#include "projectQt.h"
#include "tab2.h"
#include <QTextStream>
#include "ui_projectQt.h"
Ctab2::Ctab2(void)
{
}
void Ctab2::initGUI(){
Ui::ProjectQtClass uimain;
uimain.IDC_CMB_2_0->addItem("2000");
uimain.IDC_CMB_2_0->addItem("2001");
}
void Ctab2::OnBtnApplyClicked(){
}
Whenever you see an error along the lines of "undefined reference ...", it means it's a linker error.
This particular error means that somewhere in your program, the destructor for the Ctab1 class is being called but it can't find the implementation of that destructor in any of your source files, only the declaration.
Add the following to your tab1.cpp file:
Ctab1::~Ctab1(){
// any cleanup code placed here
}
It also seems clear that you don't fully understand variable scope in C++. (ctab11 and ctab22 only exist inside the constructor of ProjectQt
I suggest you take an introductory course in C/C++ before carrying on with your project.
I am trying to create a background application using Qt.
There is a difference of ui between running in the foreground and running as a deamon.
The style has been changed since Ubuntu 11.10.
when running the application in the Foreground
when running as a daemon
main.cpp
#include <QApplication>
#include "form.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Form *pForm = new Form();
pForm->show();
return app.exec();
}
form.h
#include <QWidget>
namespace Ui {
class form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = 0);
private:
Ui::form *ui;
};
form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) : QWidget(parent)
, ui(new Ui::form)
{
ui->setupUi(this);
ui->label->setText(QT_VERSION_STR);
}
form.ui
<?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>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>270</x>
<y>240</y>
<width>98</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>30</x>
<y>30</y>
<width>341</width>
<height>191</height>
</rect>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>50</x>
<y>240</y>
<width>141</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
It's just simple Ui to test.
I think the application in the foreground applies 'Desktop Settings' as a GUI style, but I don't know which style is used on a daemon application (image on the right above) .
Why the style is different?
In order to have unified style on any platform or envirovment you need to set application style explicitly:
QApplication app(argc, argv);
app.setStyle(new QPlastiqueStyle()); //or similar.
When I promote a QWidget to a custom widget in QtDesigner, everything compiles fine, but when I run the program the widget seems to be blank. I have made a very simple demo of this:
Form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include "ui_form.h"
class Form : public QWidget, private Ui::Form
{
Q_OBJECT
public:
Form(QWidget *parent = 0);
};
#endif
Form.cpp
#include "form.h"
Form::Form(QWidget* parent)
: QWidget(parent)
{
setupUi(this);
}
TestWidget.h
#ifndef TestWidget_H
#define TestWidget_H
#include "ui_TestWidget.h"
#include <QMainWindow>
class TestWidget : public QWidget, private Ui::TestWidget
{
Q_OBJECT
public:
TestWidget(QWidget *parent = 0);
};
#endif
TestWidget.cpp
#include "TestWidget.h"
TestWidget::TestWidget(QWidget *parent)
: QWidget(parent)
{
}
main.cpp
#include <QApplication>
#include "form.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Form form;
form.show();
return app.exec();
}
Form.ui
<?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>532</width>
<height>341</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="TestWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>130</x>
<y>90</y>
<width>161</width>
<height>121</height>
</rect>
</property>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>TestWidget</class>
<extends>QWidget</extends>
<header>TestWidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
TestWidget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TestWidget</class>
<widget class="QWidget" name="TestWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>306</width>
<height>60</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Current path:</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
When I run this, I just see a blank widget, when I would expect to see the label with "Current path:" displayed.
I just needed to add
setupUi(this);
to the TestWidget constructor.
If I were able to comment I would put this there... need more rep still though before that is an option.
Looking at the geometry of the TestWidget I see that it is 306 pixels wide:
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
**<width>306</width>**
<height>60</height>
</rect>
</property>
In Form.ui, you're giving it only 161 pixels:
<widget class="TestWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>130</x>
<y>90</y>
**<width>161</width>**
<height>121</height>
</rect>
</property>
</widget>
Might that be part of the problem - the label is there, it is just outside of the painted area for the widget?