다른 소스 파일로 Qt UI 조작
-
13-12-2019 - |
문제
나는 지금 몇 시간 동안 검색했지만 나는 해결책을 찾을 수 없다.
내 설정은 다음과 같습니다.
Widget.h
Widget.cpp
Widget.ui
Function.h
Function.cpp
.
i 위젯 .UI에서 QListWidget에 항목을 추가하는 futter.cpp에서 함수를 작성했습니다. 그것은 단지 평가판과 오류 프로젝트 일뿐입니다.
- 이미 widget.h와 ui_widget.h를 포함하여 클래스에 액세스 할 수 있습니다.
- 위젯은 QTDesigner로 만들 수있는 QWidget 템플릿입니다.
- QListWidget과 QButton이 있습니다.
QButton을 클릭하면 QListWidget에 항목을 추가하는 function.cpp의 함수를 호출합니다.
이 작업 슬롯을 작성해야합니까? 아니면 다른 방법이 있습니까?
편집 :
요청시, 여기에 코드가 있습니다.
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()
{
}
.
ive는
를 추가하려고했습니다.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
}
.
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
}
. 다른 팁
은 함수 .cpp 파일에 "mywidget.h"및 "ui_mywidget.h"를 모두 포함시켜야합니다.IT의 UI 멤버 변수에 액세스하려면 먼저 myWidget이 무엇인지 알아야합니다.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");
}
. 제휴하지 않습니다 StackOverflow