1

QMainWindow から継承する 1 つのクラスと、QWidget から継承する 2 つのクラスがあります。この 2 つの QWidget オブジェクトを QMainWindow オブジェクトに追加し、この 2 つの QWidget オブジェクト (そのうちの 1 つは QPushButton オブジェクトを含む) 間の接続を作成したいと考えました。残念ながら、働きたくありません...

コード:

メインフレーム:

#ifndef MAINFRAME_H
#define MAINFRAME_H

#include <QtGui/QMainWindow>
#include "DrawComponent.h"
#include "ControllComponent.h"

class MainFrame : public QMainWindow
{
    Q_OBJECT

public:
    DrawComponent *dComponent;
    ControllComponent *cComponent;
    MainFrame();

};

#endif 
#include "MainFrame.h"
#include "DrawComponent.h"
#include "ControllComponent.h"
#include <iostream>

using namespace std;

MainFrame :: MainFrame()
{   
    this->setGeometry(100, 100, 640, 480);

    this->dComponent = new DrawComponent(this);
    this->cComponent = new ControllComponent(this); 

    QObject::connect(this->cComponent->rysuj1, SIGNAL(clicked()), this, SLOT(dComponent->draw1));
}

最初の QWidget クラス

#ifndef DRAWCOMPONENT_H
#define DRAWCOMPONENT_H

#include <QtGui/QMainWindow>
#include <qwidget.h>

class DrawComponent : public QWidget
{
    Q_OBJECT

public:
    DrawComponent(QMainWindow *parent);

public slots:
    void draw1();

};

#endif 
#include "DrawComponent.h"
#include <qpushbutton.h>
#include <qgridlayout.h>

using namespace std;

DrawComponent :: DrawComponent(QMainWindow *parent)
{
    this->setParent(parent);
    this->setGeometry(0, 0, 500, 480);

    QPalette p(palette());
    p.setColor(QPalette::Background, Qt::black);

    this->setPalette(p);
    this->setAutoFillBackground(true);
    this->show();
}

void DrawComponent :: draw1()
{
    QPalette p(palette());
    p.setColor(QPalette::Background, Qt::blue);

    this->setPalette(p);
}

2 番目の QWidget クラス

#ifndef CONTROLLCOMPONENT_H
#define CONTROLLCOMPONENT_H

#include <QtGui/QMainWindow>
#include <qwidget.h>
#include <qpushbutton.h>

class ControllComponent : public QWidget
{
    Q_OBJECT

public:
    QPushButton *rysuj1;
    ControllComponent(QMainWindow *parent);

};

#endif 
#include "ControllComponent.h"
#include <qpushbutton.h>
#include <qgridlayout.h>

ControllComponent :: ControllComponent(QMainWindow *parent)
{
    this->setParent(parent);
    this->setGeometry(500, 0, 140, 480);

    QPalette p(palette());
    p.setColor(QPalette::Background, Qt::red);
    this->setPalette(p);
    this->setAutoFillBackground(true);

    this->rysuj1 = new QPushButton(tr("draw1"), this);
    this->rysuj1->setGeometry(45, 10, 50, 50);
    this->rysuj1->show();

    this->show();
}
4

2 に答える 2

4

clicked()からの信号をrysuj1スロットdraw1()に接続するための呼び出しdComponent

QObject::connect(this->cComponent->rysuj1, SIGNAL(clicked()), 
                 this, SLOT(dComponent->draw1));

する必要があります

QObject::connect(this->cComponent->rysuj1, SIGNAL(clicked()), 
                 dComponent, SLOT(draw1()));
于 2013-02-27T12:10:30.203 に答える
2
dComponent->draw1 

スロットではありません

ここを参照してください:

QObject::connect(&a, SIGNAL(valueChanged(int)),
                  &b, SLOT(setValue(int)));

引数: 1. オブジェクト、2. シグナル関数、3. オブジェクト、4. スロット関数。

それで

QObject::connect(this->cComponent->rysuj1, SIGNAL(clicked()), dComponent, SLOT(draw1()));
于 2013-02-27T12:10:38.837 に答える