使用不同的源文件操作 QT Ui [英] Manipulating QT Ui with different source files

查看:56
本文介绍了使用不同的源文件操作 QT Ui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了几个小时,但找不到解决方案.

I searched for some hours now but I'm not able to find a solution.

我的设置如下:

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

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

I wrote a function in my Function.cpp which adds some entries to a QListWidget in my Widget.ui. It's just a trial and error project:

  • 我已经包含了 widget.h 和 ui_widget.h 以便我可以访问这些类.
  • Widget 是您可以使用 QtDesigner 创建的 QWidget 模板.
  • 其中有一个 QListWidget 和一个 QButton.

如果我单击 QButton,它会调用 Function.cpp 中的函数,该函数将向 QListWidget 添加一个项目.

If I click the QButton then it calls the function in Function.cpp which will add an item to the QListWidget.

我必须为此编写自定义插槽还是有其他方法?

Do i have to write a custom slot for this or is there another way?

根据要求,这是代码.

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

The 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

The 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>

函数.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 进行了尝试.在这种情况下我很有创意^^

I tried this with and without Q_OBJECT in the Functions class in different variations. I was very creative in this case ^^

我希望这有助于理解?

推荐答案

试试这个:

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
}

函数.h

[...]

class myWidget; // Forward declaration

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

和 Function.cpp

And the 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
}

这篇关于使用不同的源文件操作 QT Ui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆