質問

今数時間検索しましたが、私は解決策を見つけることができません。

私の設定は次のとおりです。

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

私はmy function.cppに関数を書きました。それは単なる試用とエラープロジェクトです:

  • 私はすでにwidget.hとui_widget.hを含めてクラスにアクセスできます。
  • ウィジェットはQtdesignerで作成できるQwidgetテンプレートです。
  • QlistWidgetとQButtonがあります。

QButtonをクリックすると、abset.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()
{
}
.

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
}
.

関数.h

[...]

class myWidget; // Forward declaration

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

と関数.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"の両方を含める必要があります。UIメンバー変数にアクセスするために、MyWidgetが何であるかを知っておく必要があります。UI変数に含まれるものを知っている2番目が必要です。

これらの線に沿った何かは、おそらくあなたが期待していないとしても、働くべきです:

#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");
}
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top