• 7306阅读
  • 4回复

关于多窗口信号和槽发射的问题 [复制链接]

上一主题 下一主题
离线水畔成林
 

只看楼主 倒序阅读 楼主  发表于: 2010-08-24
— 本帖被 XChinux 执行加亮操作(2010-09-02) —
//main.cpp
#include <QApplication>
#include "InputInterface.h"
#include "Screen.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Interface *in = new Interface;
Screen *display = new Screen;

in->show();
display->show();
QObject::connect(in,SIGNAL(signal_getData(QString &)),
display,SLOT(slot_getData(QString &)));
return a.exec();
}
//InputIterface.h
#ifndef INPUTINTERFACE_H
#define INPUTINTERFACE_H

#include <QtGui/QDialog>

class QLineEdit;
class QPushButton;
class QLabel;

class Interface : public QDialog
{
Q_OBJECT
public:
Interface(QWidget *parent = 0);

signals:
void signal_emitDataStr();
void signal_getData(QString &str);
public slots:
void slot_emitData();

private:
QLabel *label;
QLineEdit *lineEdit;
QPushButton *emitButton;
};
//InputInterface.cpp
#include <QtGui/QWidget>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QDialog>

#include "InputInterface.h"


Interface::Interface(QWidget *parent)
:QDialog(parent)
{

QHBoxLayout *mainLayout = new QHBoxLayout;

label = new QLabel(tr("&Command"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
emitButton = new QPushButton(tr("emit &command"));
emitButton->setDefault(true);


mainLayout->addWidget(label);
mainLayout->addWidget(lineEdit);
mainLayout->addWidget(emitButton);
setLayout(mainLayout);
setWindowTitle(tr("Inter face"));
setFixedHeight(sizeHint().height());

connect(emitButton,SIGNAL(clicked()),this,SIGNAL(signal_emitDataStr()));
}

void Interface::slot_emitData()
{
QString text = label->text();
emit signal_getData(text);
}

//screen.h
#ifndef SCREEN_H
#define SCREEN_H

#include <QtGui/QDialog>


class QVBoxLayout;
class QLabel;

class Screen : public QDialog
{

Q_OBJECT
public:
Screen(QWidget *parent = 0);

/*signals:
void emitDataStr(QString &str);
*/
public slots:
void slot_getData(QString &str);
private:
QLabel *label;
};

















#endif // SCREEN_H

//screen.cpp
#include <QtGui/QWidget>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QString>
#include <QtGui/QDialog>

#include "Screen.h"

//class InputInterface;

Screen::Screen(QWidget *parent)
: QDialog(parent)
{
label = new QLabel(tr("The input string will display in here"));

QHBoxLayout *mainLayout = new QHBoxLayout;

mainLayout->addWidget(label);
setLayout(mainLayout);
setWindowTitle(tr("Screen"));
setFixedHeight(sizeHint().height());
}

void Screen::slot_getData(QString &str)
{
label->clear();
label->setText(str);
}

以上的代码要实现的功能是screen要显示InputIterface里在lineedit上输入的字符串。
字符串输入完成后,通过按下emit按钮,来让screen显示出来。
我做这个实验是想验证一下槽和信号的使用。但是现在发现screen里的槽无法被触发。
想请问一下,这里写的代码有问题不,望大家抽空给个指教吧 。
离线dbzhang800

只看该作者 1楼 发表于: 2010-08-24
QObject::connect(in,SIGNAL(signal_getData(QString &)),
display,SLOT(slot_getData(QString &)));

运行时,程序会提示你有问题的,仔细看控制台输出(如果没有控制台,调试程序时不妨将其调出来)
离线水畔成林

只看该作者 2楼 发表于: 2010-08-24
引用第1楼dbzhang800于2010-08-24 14:25发表的 :
QObject::connect(in,SIGNAL(signal_getData(QString &)),
display,SLOT(slot_getData(QString &)));
运行时,程序会提示你有问题的,仔细看控制台输出(如果没有控制台,调试程序时不妨将其调出来)

这里 我修改为:
QObject::connect(emitButton,SIGNAL(clicked()),this,SIGNAL(signal_emitDataStr()));
编译以后运行时 控制台没有显示任何错误提示,但是 我的字符还是无法在screen上显示。
等待高手支招
离线linlin3233
只看该作者 3楼 发表于: 2010-08-24
你的代码中Screen类的槽slot_getData(QString &str)是和 interface类的signal_getData(QString &)信号connect在一起的, 可是我并没有看到signal_getData(QString &)这个信号在哪里被触发.
我觉得应该是你的connect(emitButton,SIGNAL(clicked()),this,SIGNAL(signal_emitDataStr())); 这句话写的不对.
改成 connect(emitButton,SIGNAL(clicked()),this,SLOT(slot_emitData())); 试试

离线水畔成林

只看该作者 4楼 发表于: 2010-08-24
回楼上的,谢谢你, 是这个问题。
因为我定义的 是在槽里发射一个信号的。
现在改为
// InputInterface.cpp
#include <QtGui/QWidget>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QDialog>

#include "InputInterface.h"


Interface::Interface(QWidget *parent)
    :QDialog(parent)
{

    QHBoxLayout *mainLayout = new QHBoxLayout;

    label = new QLabel(tr("&Command"));
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);
    emitButton = new QPushButton(tr("emit &command"));
    emitButton->setDefault(true);


    mainLayout->addWidget(label);
    mainLayout->addWidget(lineEdit);
    mainLayout->addWidget(emitButton);
    setLayout(mainLayout);
    setWindowTitle(tr("Inter face"));
    setFixedHeight(sizeHint().height());

   connect(emitButton,SIGNAL(clicked()),this,SLOT(slot_emitData()));
}

void Interface::slot_emitData()
{
    QString text = lineEdit->text();
    emit signal_getData(text);
}


程序可以正确执行了
快速回复
限100 字节
 
上一个 下一个