使用不同的源文件操作QT Ui

时间:2012-03-14 00:52:41

标签: c++ qt4 qt-creator qlistwidget

我现在搜索了几个小时但我无法找到解决方案。

我的设置如下:

Widget.h
Widget.cpp
Widget.ui
Function.h
Function.cpp

我在我的Function.cpp中编写了一个函数,它在Widget.ui的QListWidget中添加了一些条目。这只是一个试错项目:

  • 我已经包含了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>

The 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");

我在不同变体中的Functions类中使用和不使用Q_OBJECT尝试了这个。在这种情况下我非常有创意^^

我希望这有助于理解?

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

您可能需要在functions.cpp文件中包含“myWidget.h”和“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");
}
相关问题