Qt,两个QWidget对象之间的简单连接
Qt, a simple connection between two QWidget objects
我有一个从QMainWindow继承的类和两个从QWidget继承的类。我将这两个QWidget对象添加到了我的QMainWindow对象中,并且我想在这两个QDidget对象之间创建一个连接(其中一个包含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));
}
FIRST 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);
}
第二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();
}
您的呼叫将clicked()
信号从rysuj1
连接到dComponent
的draw1()
插槽
QObject::connect(this->cComponent->rysuj1, SIGNAL(clicked()),
this, SLOT(dComponent->draw1));
需要
QObject::connect(this->cComponent->rysuj1, SIGNAL(clicked()),
dComponent, SLOT(draw1()));
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()));
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 当套接字连接断开时检测C/C++Unix
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- 关于简单C++函数(is_palindrome)的逻辑的问题
- Qt的新信号/时隙语法问题 - 连接到一个简单的函数
- 从简单连接的列表中删除最大值
- 我正在编写一个简单的客户端套接字应用程序,但在连接后服务器收到一个空缓冲区
- 查找树(不属于任何特定类型的简单连接树)中两个节点之间的路径
- NAS(使用 qt/linux)- 无法建立简单的音频服务器连接测试
- 有没有办法简单地连接多个向量
- 一个简单的 C++ 连接/拆分字符串和 char 数组中的整数
- 通过C++中的SSL实现简单的数据库连接
- 简单的HTTPS服务器和保持连接
- 如何从Linux上的c++连接并执行一个简单的查询到SQL Server
- 为什么这个简单的服务器不接受连接?
- Qt,两个QWidget对象之间的简单连接