使用不同的源文件操作QT Ui
Manipulating QT Ui with different source files
我搜索了几个小时,但找不到解决方案。
我的设置如下:
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");
}
相关文章:
- 在 QT C++中绘制/操作多边形
- QT 窗口模式禁用菜单中的操作
- Qt,操作缓慢时不要冻结GUI输入元素
- 如何防止GUI挂起,同时允许第二次操作与Qt中的第一次操作一起执行
- 在单击QT中单击图像时,尝试立即显示用户编辑操作的结果
- 尝试在 QT 中控制文件-QMenu 操作
- QT从Qmenu删除操作,以后再添加
- 菜单打开时,菜单中的C QT操作不会由键盘快捷键触发
- Qt 创建者,跟踪:不允许操作.什么是永久解决方案
- 在使用Qt修改任何复选框状态时执行操作
- 如何使用QT Designer?实施最新的文件操作
- 如何在Qt中获取文件系统操作的上下文菜单
- 设置和操作QT中的图标
- 在Qt上,如何在运行时更改工具栏中操作的图标
- Qt - 解决一个插槽上的两个顺序调用,并且仅执行一次操作
- 如何将操作连接到 C++/Qt 中的值更改
- Qt - 在像素操作后显示无符号字符
- Qt C++,点击 QPushButton 时动态执行的操作
- Qt - 如何从应用程序内部初始化某些操作
- 使用不同的源文件操作QT Ui