I searched for some hours now but I'm not able to find a solution.
My setup is as follows:
Widget.h
Widget.cpp
Widget.ui
Function.h
Function.cpp
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:
- I already included widget.h and ui_widget.h so I can access the classes.
- The Widget is the QWidget template which you can create with the QtDesigner.
- In there is a QListWidget and a QButton.
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?
EDIT:
As requested, here is the code.
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
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");
}
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>
The Functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
class Functions
{
public:
Functions();
};
#endif // FUNCTIONS_H
And the Functions .cpp
#include "functions.h"
#include "myWidget.h" //there seems no effect between ui_mywidget.h and this one ...
Functions::Functions()
{
}
Ive tried to add
Ui::myWidget *ModUi = new myWidget;
ModUi->ui->listWidget->addItem("SomeItem");
I tried this with and without Q_OBJECT in the Functions class in different variations. I was very creative in this case ^^
I hope this helps to understand?
You probably need to include both "myWidget.h" and "ui_mywidget.h" in your functions.cpp file. You need the first to know what myWidget is, in order to access it's ui member variable. You need the second to know what the ui variable contains.
Something along these lines should work, although probably not as you expect:
Try this:
And implementation:
The Function.h
And the Function.cpp