1

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

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

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

Widget.uiのQListWidgetにいくつかのエントリを追加する関数をFunction.cppに記述しました。これは単なる試行錯誤のプロジェクトです。

  • クラスにアクセスできるように、widget.hとui_widget.hをすでに含めました。
  • ウィジェットは、QtDesignerで作成できるQWidgetテンプレートです。
  • QListWidgetとQButtonがあります。

QButtonをクリックすると、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>

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()
{
}

追加しようとしました

Ui::myWidget *ModUi = new myWidget;
ModUi->ui->listWidget->addItem("SomeItem");

さまざまなバリエーションのFunctionsクラスでQ_OBJECTを使用して、または使用せずにこれを試しました。この場合、私は非常に創造的でした^^

これが理解に役立つことを願っていますか?

4

2 に答える 2

3

これを試して:

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
}
于 2012-03-15T11:55:07.050 に答える
0

おそらく、functions.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");
}
于 2012-03-14T19:19:55.207 に答える