I am new to qt So I don't have much knowledge of sizepolicy and streches of widgets.
I have a vertical box layout in which I have first added a Qlabel and then a QTreeWidget and then again a Qlabel. Now the problem is QTreeWidget is taking full space in vertical direction and then after leaving a very big space after QTreeWidget, although only rows exist currently in it, QLabel is added. I want that second Qlabel is added immediately after two rows visible and now when more rows are added then It shift downwards. Can someone suggest me how to do this?
I think this is what you meant:
UI has in a layout: Label, QTreeWidget, Label, Spacer
(Spacer is important becaus else Qt might just expand the labels to fill the window.)
Important: In Designer TreeWidget hight to preffered, vertical scrollbar off.
UI Example:
<?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>517</width>
<height>568</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="my_tree" name="treeView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAsNeeded</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>TextLabel</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>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>517</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"/>
<customwidgets>
<customwidget>
<class>my_tree</class>
<extends>QTreeView</extends>
<header>my_tree.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
A Overloaded QTreeView (in your case QTreeWidget) is used to get the wanted sizehints:
EDIT -- This is a uggly header only definition i called my_tree.h --
#ifndef MY_TREE_H
#define MY_TREE_H
#include <QHeaderView>
#include <QTreeView>
class my_tree: public QTreeView
{
Q_OBJECT
public:
my_tree(QWidget* parent):QTreeView(parent)
{
// Important: if something is shown/hidden we need a new size
connect(this,SIGNAL(expanded( const QModelIndex & )),SLOT(onExpandCollapsed()));
connect(this,SIGNAL(collapsed( const QModelIndex & )),SLOT(onExpandCollapsed()));
};
// QWidget interface
public:
QSize sizeHint() const {return my_size(); };
QSize minimumSizeHint() const { return my_size(); };
private:
QSize my_size() const
{ //QSize tst(sizeHintForColumn(0) + 2 * frameWidth(), sizeHintForRow(0) + 2 * frameWidth());
int neededHight= 2 * frameWidth()+ this->header()->height();
QModelIndex root = this->rootIndex();
QAbstractItemModel* m = this->model();
//if(this->rootIsDecorated())
{
neededHight += recursiveHeightHint(root,m);
}
QSize temp = QTreeView::sizeHint();
temp.setHeight(neededHight);
return QSize(1,neededHight);
}
// we need the size of all visible items -> isExpanded
// the root item is usually shown as a non-Valid index -> !i.isValid()
int recursiveHeightHint(QModelIndex i,QAbstractItemModel* m) const
{
int temp=sizeHintForIndex(i).height();
if(this->isExpanded(i) || !i.isValid())
{
if(m->hasChildren(i))
{
int numRows = m->rowCount(i);
for(int count =0;count<numRows;count++)
temp+=recursiveHeightHint(m->index(count,0,i),m);
}
}
return temp;
}
private slots:
void onExpandCollapsed(){updateGeometry();}
};
#endif // MY_TREE_H
PS: in Designer first place the base Widget and then define it as placeholder for the self designed one.
I used this entry to get to this solution:
QListWidget adjust size to content
Related
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);
In the first picture I designed a prototype. The MainWindow includes widget and three frames. The center frame has another frame, and when I run in the application in full screen there is a size problem in the center frame, as you can see in the second picture. I want to center the frame in its frame. How can I do that?
You can do it easily from Qt Designer. Just add another Horizontal Layout into the middle Frame. Then add your Widget into the layout, go to the Object Manager, call context menu on your widget, click Layout Alignment and choose Center horizontally.
Here is the .ui file which I have just created to check your problem:
<?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>965</width>
<height>502</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QFrame" name="frame_2">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
<item>
<widget class="QFrame" name="frame_3">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QWidget" name="horizontalLayoutWidget_2">
<property name="geometry">
<rect>
<x>9</x>
<y>9</y>
<width>291</width>
<height>411</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item alignment="Qt::AlignHCenter|Qt::AlignVCenter">
<widget class="QListWidget" name="listWidget"/>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>965</width>
<height>23</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>
Edition 1: Basically with above example I managed to reproduce your bug. I have added horizontal layout inside middle frame but while changing the size middle widget has aligned to the left side.
The answer on your question is here: Make QHorizontalLayout expand inside QFrame
You have to add Horizontal Layout into your middle Frame. Then you have to call context menu of the Middle Frame, click Layout -> Layout Horizontally. Add your widget in the layout. Most probably you widget will expand its width to the width of the Main Window. Change SizePolicy to fixed. At this point your problem should be solved.
you can create programatically like this:
overwrite this functions: QSize sizeHint() and QSize minimumSizeHint()
here an example:
.h
#ifndef LATERALBOARD_H
#define LATERALBOARD_H
#include <QBasicTimer>
#include <QFrame>
#include <QPointer>
QT_BEGIN_NAMESPACE
class QLabel;
QT_END_NAMESPACE
class LateralBoard : public QFrame
{
Q_OBJECT
public:
LateralBoard(QWidget *parent = nullptr);
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
private:
enum { BoardWidth = 10, BoardHeight = 12 };
};
#endif
.cpp
#include <QKeyEvent>
#include <QLabel>
#include <QPainter>
LateralBoard::LateralBoard(QWidget *parent)
: QFrame(parent)
{
setFrameStyle(QFrame::Panel | QFrame::Sunken);
setFocusPolicy(Qt::StrongFocus);
}
QSize LateralBoard::sizeHint() const
{
return QSize(100, 100);
}
QSize LateralBoard::minimumSizeHint() const
{
return QSize(BoardWidth * 5 + frameWidth() * 2,
BoardHeight * 5 + frameWidth() * 2);
}
and in your main window you should use a layout to set the position
QGridLayout *layout = new QGridLayout;
LateralBoard *lateralBoard = new LateralBoard;
layout->addWidget(lateralBoard, 2, 6);
setLayout(layout);
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.