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

Manipulating QT Ui with different source files

本文关键字:操作 QT Ui 源文件      更新时间:2023-10-16

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

我的设置如下:

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

如果我点击Q按钮,它会调用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>

函数.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的不同变体。在这种情况下我很有创意^^

我希望这有助于理解?

试试这个:

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

[...]
#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
}

您可能需要在functions.cpp文件中同时包含"myWidget.h"answers"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");
}