• 4910阅读
  • 4回复

qt4自定义信号与槽的问题,请指教,谢谢。 [复制链接]

上一主题 下一主题
离线timdeng
 
只看楼主 正序阅读 楼主  发表于: 2009-04-03
在用qt4实现自定义信号与槽函数的时候,出现了点击后没反应的问题(而把这个信号于槽的连接用designer的编辑器来实现就正常运行了),不晓得问题出在哪里,请高手指教一下哈。
首先:用designer生成一个test.ui文件,内有一个按纽pushButton,一个文本框textedit。想实现的功能是点击按纽,textedit可以做显示等操作。代码如下:
.......................................................mywindow.h
#ifndef MYWINDOW_H
#define MYWINDOW_H

#include <QMainWindow>

#include "ui_test.h"  //test.ui 生成的头文件

class MyWindow : public QMainWindow, public Ui_MainWindow
{
    Q_OBJECT
    
public:
    MyWindow(QWidget *parent=0);

private slots:
    void show();
};

#endif
............................................................


.........................................................mywindow.cpp
#include <QtGui>

#include "mywindow.h"

MyWindow::MyWindow(QWidget *parent)
    :QMainWindow(parent)
{
    setupUi(this);

    connect(pushButton, SIGNAL(clicked()), textEdit, SLOT(show()));
}


void MyWindow::show()
{
    textEdit->close();
}
...............................................................


.............................................................main.cpp
#include <QApplication>
#include <QMainWindow>

#include "mywindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MyWindow ui;
    QMainWindow *mainWindow = new QMainWindow;
    ui.setupUi(mainWindow);
    mainWindow->show();

    return app.exec();
}
离线timdeng
只看该作者 4楼 发表于: 2009-04-07
今天终于解决了这个问题。
问题出在main函数中,将原来main.cpp改为
一般的qt4的main函数就正常了。
离线sufangchao
只看该作者 3楼 发表于: 2009-04-03
呵呵,我没用过designer,自己写不会出错的,你只写了一部分代码,我也不知道有啥问题了。
还有你的MyWindow::show()实现是:textEdit->close();让我猜都不好猜了,呵呵,等高手吧
离线timdeng
只看该作者 2楼 发表于: 2009-04-03
试过了,一样没反映。
其实我自己写的就是
connect(pushButton, SIGNAL(clicked()), this, SLOT(show()));
后来没反应,我就把它用designer里的信号与槽编辑器来生成,它就是用的
connect(pushButton, SIGNAL(clicked()), textEdit, SLOT(show()));
然后就正常了。我就把这条语句复制到mywindow.cpp,又没反映了。
离线sufangchao
只看该作者 1楼 发表于: 2009-04-03
connect错啦
show()是MyWindow::的,不是textEdit对象的。
换成这样看看:
connect(pushButton, SIGNAL(clicked()), this, SLOT(show()));
快速回复
限100 字节
 
上一个 下一个