简体   繁体   English

Qt,两个QWidget对象之间的简单连接

[英]Qt, a simple connection between two QWidget objects

I have one class that inherits from QMainWindow and two that inherits from QWidget. 我有一个从QMainWindow继承的类,还有两个从QWidget继承的类。 I added this two QWidget objects into my QMainWindow object and i wanted to create a connection between this two QWidget object(one of them contains QPushButton object). 我将这两个QWidget对象添加到我的QMainWindow对象中,并且希望在这两个QWidget对象之间创建连接(其中一个包含QPushButton对象)。 unfortunately, doesn't want to work... 不幸的是,不想工作...

CODE: 码:

MAINFRAME: 主框架:

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

FIRST QWidget class 第一个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);
}

SECOND QWidget class 第二个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();
}

Your call to connect the clicked() signal from rysuj1 to the draw1() slot of dComponent 您将rysuj1clicked()信号连接到rysuj1draw1()插槽的dComponent

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

needs to be 需要是

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

is not a slot 不是插槽

see Here: 看这里:

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

arguments: 1. object, 2. Signal-function, 3. an object, 4. a slot function. 参数:1.对象,2.信号功能,3.对象,4.插槽功能。

so 所以

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM