2012-03-14 15 views
1

我搜索了幾個小時,但我無法找到解決方案。使用不同的源文件處理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在不同的變化的函數類。我在這種情況下很有創意^^

我希望這有助於理解?

+1

你嘗試你寫?有沒有工作? – 2012-03-14 02:03:36

+0

請提供一些示例代碼和/或告訴我們出了什麼問題。 – 2012-03-14 04:25:02

+0

添加一個自定義插槽是在qt中做東西的標準方式。 – 2012-03-14 08:25:27

回答

3

試試這個:

class Functions; // Forward declaration  
class myWidget : public QWidget 
{ 
    Q_OBJECT 
public: 
    explicit myWidget(QWidget *parent = 0); 
    ~myWidget(); 

private slots: 
    void on_pushButton_clicked(); 

private: 
    Ui::myWidget *ui; 
    Functions*fun; // member ptr 
    friend class Functions; // allow access to private members 
}; 

與實現

[...] 
#include "Functions.h" 

myWidget::myWidget(QWidget *parent) : 
    QWidget(parent), 
    ui(new Ui::myWidget), 
    fun(new Functions(this)) // initializer list 
{ 
    ui->setupUi(this); 
} 

myWidget::~myWidget() 
{ 
    delete ui; 
    delete fun; // we new'ed it so we have to delete it! 
} 

void myWidget::on_pushButton_clicked() 
{ 
    fun->doIt(); // call to the function 
} 

的Function.h

[...] 

class myWidget; // Forward declaration 

class Functions 
{ 
public: 
    Functions(myWidget *wid); 
    void doIt(); 
private: 
    myWidget *widget; // Member pointer to the main widget 
}; 

而且Function.cpp

[...] 

#include "ui_mywidget.h"   
#include "myWidget.h" 

Functions::Functions(myWidget *wid):widget(wid) // init the ptr 
{ 
} 

void Function::doIt()    
{ 
    widget->ui->listWidget->addItem("SomeItem"); // add the items 
} 
+0

至少它顯示了自動完成列表中的ui元素。但它不能編譯: functions.cpp:在成員函數'int Functions :: doIt()'中: functions.cpp:11:14:error:使用不完整類型'struct Ui :: myWidget' mywidget.h:7:7:錯誤:向前聲明'struct Ui :: myWidget' functions.cpp:12:1:warning:無返回語句返回非void [-Wreturn-type] make:* ** [functions.o]錯誤1 任何想法? – yokmp 2012-03-15 22:05:29

+0

是的,它現在的作品!非常感謝。我希望他們改進QtCreator以使這些事情變得更容易。 – yokmp 2012-03-16 12:49:10

+1

這是基本的C++理解。我想沒有辦法讓它更容易。 – Ich 2012-03-16 14:07:32

0

你可能需要包括「myWidget.h」,並在你的functions.cpp文件「ui_mywidget.h」。您需要第一個知道myWidget是什麼,才能訪問它的ui成員變量。您需要第二個知道ui變量包含的內容。

沿着這些線路的東西應該工作,雖然可能不像你想象:

#include "functions.h" 
#include "myWidget.h" 
#include "ui_mywidget.h" 

Functions::Functions() 
{ 
    // The following line creates a new widget, and does not use any existing 
    // widgets (like you probably expect it to). 
    myWidget *ModUi = new myWidget; 
    ModUi->ui->listWidget->addItem("SomeItem"); 
} 
+0

它生成沒有抱怨,但當我嘗試啓動時崩潰。 )= 您的意思是說,我必須將「創建myWidgets用戶界面」填入Functions.cpp中? – yokmp 2012-03-15 04:33:56

+0

不,創建小部件應該沒問題。函數類是在主應用程序線程中創建的嗎?你的系統在崩潰時說什麼?調試器顯示你什麼? – 2012-03-15 16:17:37

+0

很抱歉,但我不記得所有細節。但我很確定,應用程序不會啓動。它立刻崩潰了。 – yokmp 2012-03-15 22:31:30