我搜索了幾個小時,但我無法找到解決方案。使用不同的源文件處理QT Ui
我的設置如下:
Widget.h
Widget.cpp
Widget.ui
Function.h
Function.cpp
我寫在我的Function.cpp一個函數,它增加了一些條目到QListWidget我Widget.ui。這只是一個試驗和錯誤項目:
- 我已經包含了widget.h和ui_widget.h,所以我可以訪問這些類。
- Widget是您可以使用QtDesigner創建的QWidget模板。
- 有一個QListWidget和一個QButton。
如果我點擊QButton,它會調用Function.cpp中的函數,它會將一個項目添加到QListWidget中。
我必須爲此編寫一個自定義插槽還是有其他方法?
編輯:
按照要求,這裏是代碼。
myWidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
namespace Ui {
class myWidget;
}
class myWidget : public QWidget
{
Q_OBJECT
public:
explicit myWidget(QWidget *parent = 0);
~myWidget();
private slots:
void on_pushButton_clicked();
private:
Ui::myWidget *ui;
};
#endif // MYWIDGET_H
的myWodget.cpp
#include "mywidget.h"
#include "ui_mywidget.h"
myWidget::myWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::myWidget)
{
ui->setupUi(this);
}
myWidget::~myWidget()
{
delete ui;
}
void myWidget::on_pushButton_clicked()
{
ui->listWidget->addItem("Some Item");
}
的myWidget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>myWidget</class>
<widget class="QWidget" name="myWidget">
<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="QListWidget" name="listWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>256</width>
<height>192</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>110</x>
<y>240</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>add</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
的Functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
class Functions
{
public:
Functions();
};
#endif // FUNCTIONS_H
及其功能的.cpp
#include "functions.h"
#include "myWidget.h" //there seems no effect between ui_mywidget.h and this one ...
Functions::Functions()
{
}
香港專業教育學院嘗試添加
Ui::myWidget *ModUi = new myWidget;
ModUi->ui->listWidget->addItem("SomeItem");
我想這與不Q_OBJECT在不同的變化的函數類。我在這種情況下很有創意^^
我希望這有助於理解?
你嘗試你寫?有沒有工作? – 2012-03-14 02:03:36
請提供一些示例代碼和/或告訴我們出了什麼問題。 – 2012-03-14 04:25:02
添加一個自定義插槽是在qt中做東西的標準方式。 – 2012-03-14 08:25:27