• 5895阅读
  • 17回复

[提问]Qt信号槽的问题,求大神指点,小弟在此谢过里!!! [复制链接]

上一主题 下一主题
离线liuligang88
 

只看楼主 倒序阅读 楼主  发表于: 2015-11-10
我在Qt的ui里放入两个frame,frame1和frame2,然后在frame1 里放入一个button,在frame2 里又放入一个子frame3,但是是不可见的,当按下button时,再让frame3可见。下面是我的函数,但是信号槽不响应,是怎么回事???求大神指点
//构造函数
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{    
       ui->setupUi(this);  
       QFrame *frame1 = new QFrame(this);  
       QFrame *frame2 = new QFrame(this);  
       QPushButton *button12 = new QPushButton("Bad Block",this);    
       QFrame *DeviceFrame = new QFrame(frame2);      
       DeviceFrame->setGeometry(160,0,550,452);        
       DeviceFrame->setStyleSheet("background-color:rgb(255, 255, 255);");        
       DeviceFrame->setVisible(false);
       connect(button12,SIGNAL(clicked()),this,SLOT(DeviceFrame_show()));
}
void Widget::DeviceFrame_show()//槽函数
{
        this->DeviceFrame->setVisible(true);
}
//这个是在widget.h里的
public slots:          
void DeviceFrame_show();

我发现是槽函数的问题,因为我把connect(button12,SIGNAL(clicked()),this,SLOT(DeviceFrame_show()));改为connect(button12,SIGNAL(clicked()),this,SLOT(close()));就可以实现关闭操作,而这个槽函数DeviceFrame_show()是我定义的
求解答??????
离线z609932088

只看该作者 1楼 发表于: 2015-11-10
显示,
你应该调用的是this->DeviceFrame->show();
有阳光的地方就是青春
离线liuligang88

只看该作者 2楼 发表于: 2015-11-10
回 z609932088 的帖子
z609932088:显示,
你应该调用的是this->DeviceFrame->show(); (2015-11-10 09:58) 

恩,非常感谢,但是我改了,还是不行
离线z609932088

只看该作者 3楼 发表于: 2015-11-10
回 liuligang88 的帖子
liuligang88:恩,非常感谢,但是我改了,还是不行 (2015-11-10 10:03) 

试试在fram上面加一个Button了
有阳光的地方就是青春
离线z609932088

只看该作者 4楼 发表于: 2015-11-10
DeviceFrame->setVisible(false); 这句话注掉
有阳光的地方就是青春
离线z609932088

只看该作者 5楼 发表于: 2015-11-10
隐藏用的是hide吧,
有阳光的地方就是青春
离线liuligang88

只看该作者 6楼 发表于: 2015-11-10
回 z609932088 的帖子
z609932088:隐藏用的是hide吧, (2015-11-10 10:11) 

//构造函数里的  
    QFrame *DeviceFrame = new QFrame(frame2);
     DeviceFrame->setGeometry(160,0,550,452);
     DeviceFrame->setStyleSheet("background-color:rgb(255, 255, 255);");
     //DeviceFrame->setVisible(false);
     //DeviceFrame->setHidden(true);
     DeviceFrame->hide();
     connect(button12,SIGNAL(clicked()),this,SLOT(DeviceFrame_show()));

//槽函数
void Widget::DeviceFrame_show()
{

    //this->DeviceFrame->setVisible(true);
    this->DeviceFrame->show();
}

是这样吗?还是不行,会弹出让关闭程序对话框,好像和什么冲突似得
离线z609932088

只看该作者 7楼 发表于: 2015-11-10
DeviceFrame->show();
这么写,this也去掉了
有阳光的地方就是青春
离线z609932088

只看该作者 8楼 发表于: 2015-11-10
DeviceFrame   另外,试试把他弄成全局的
有阳光的地方就是青春
离线liuligang88

只看该作者 9楼 发表于: 2015-11-10
回 z609932088 的帖子
z609932088:DeviceFrame   另外,试试把他弄成全局的 (2015-11-10 10:25) 

//这是widget.h里的
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    QFrame *frame1;
    QFrame *frame2;
    QFrame *DeviceFrame;
    QFrame *BadBlockFrame;
    QFrame *DetailInfoFrame;
    QFrame *FlashInfoFrame;
    QFrame *EraseCountFrame;
    QFrame *SMARTFrame;

    QPushButton *button11;
    QPushButton *button12;
    QPushButton *button13;
    QPushButton *button14;
    QPushButton *button15;
    QPushButton *button16;
这也算全局变量里吧。可还是不行。。。
离线realfan

只看该作者 10楼 发表于: 2015-11-10
QFrame *DeviceFrame = new QFrame(frame2);  
这样写,DeviceFrame是局部变量
this->DeviceFrame->show();中的DeviceFrame是成员变量
离线liuligang88

只看该作者 11楼 发表于: 2015-11-10
回 realfan 的帖子
realfan:QFrame *DeviceFrame = new QFrame(frame2);  
这样写,DeviceFrame是局部变量
this->DeviceFrame->show();中的DeviceFrame是成员变量
 (2015-11-10 12:31) 

非常感谢@论坛版主,我把我程序重新发一下。。。你看看

//构造函数里的代码
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
     ui->setupUi(this);
    
     QFrame *frame1 = new QFrame(this);
     QFrame *frame2 = new QFrame(this);
     QPushButton *button12 = new QPushButton("Bad Block",this);
     QFrame *DeviceFrame = new QFrame(frame2);
     DeviceFrame->setGeometry(160,0,550,452);
     DeviceFrame->setStyleSheet("background-color:rgb(255, 255, 255);");
     //DeviceFrame->setVisible(false);
     //DeviceFrame->setHidden(true);
     DeviceFrame->hide();

     connect(button12,SIGNAL(clicked()),this,SLOT(DeviceFrame_show()));
}
//槽函数
void Widget::DeviceFrame_show()
{

    //this->DeviceFrame->setVisible(true);
    DeviceFrame->show();
}
//widget.h文件
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

    QFrame *frame1;
    QFrame *frame2;
    QFrame *DeviceFrame;
    QFrame *BadBlockFrame;
    QFrame *DetailInfoFrame;
    QFrame *FlashInfoFrame;
    QFrame *EraseCountFrame;
    QFrame *SMARTFrame;

    QPushButton *button11;
    QPushButton *button12;
    QPushButton *button13;
    QPushButton *button14;
    QPushButton *button15;
    QPushButton *button16;


public slots:
     void DeviceFrame_show();
}
离线realfan

只看该作者 12楼 发表于: 2015-11-11
回 liuligang88 的帖子
liuligang88:非常感谢@论坛版主,我把我程序重新发一下。。。你看看
//构造函数里的代码
Widget::Widget(QWidget *parent) :
....... (2015-11-10 17:54) 

构造函数中
QFrame *DeviceFrame = new QFrame(frame2);
这行,QFrame *去掉,改成
DeviceFrame = new QFrame(frame2);

另外,frame1,frame2已经是成员变量了,构造函数中,前面也不要加QFrame*了,或者删掉头文件中的声明
离线liuligang88

只看该作者 13楼 发表于: 2015-11-11
回 realfan 的帖子
realfan:构造函数中
QFrame *DeviceFrame = new QFrame(frame2);
这行,QFrame *去掉,改成
DeviceFrame = new QFrame(frame2);
....... (2015-11-11 09:31) 

恩,非常感谢你@realfan  ,这次可以实现了,但是我不明白为什么这样就可以了,难道头文件声明了某个类,下面就这么写DeviceFrame = new QFrame(frame2),不能加QFrame*了?
本帖提到的人: @realfan
离线realfan

只看该作者 14楼 发表于: 2015-11-11
回 liuligang88 的帖子
liuligang88:恩,非常感谢你@realfan    ,这次可以实现了,但是我不明白为什么这样就可以了,难道头文件声明了某个类,下面就这么写DeviceFrame = new QFrame(frame2),不能加QFrame*了?
 (2015-11-11 13:41) 

构造函数中的QFrame *DeviceFrame = new QFrame(frame2);
DeviceFrame 是局部变量
槽函数中,
void Widget::DeviceFrame_show()
{

    //this->DeviceFrame->setVisible(true);
    DeviceFrame->show();
}
DeviceFrame是成员变量,按理说你的程序跑起来,应该会有异常。因为之前你的成员变量DeviceFrame是个野指针。
你的错误是C++方面的,而不是Qt方面的。建议阅读一下C++变量作用范围与生命期之类的文章。
本帖提到的人: @realfan
离线liuligang88

只看该作者 15楼 发表于: 2015-11-11
回 realfan 的帖子
realfan:构造函数中的QFrame *DeviceFrame = new QFrame(frame2);
DeviceFrame 是局部变量
槽函数中,
void Widget::DeviceFrame_show()
....... (2015-11-11 13:59) 

哦,谢谢了,真的非常感谢这么耐心的讲解。。。
我还想问一下Qt里怎么调用子函数?好比如下
void Widget::ButtonChange()//Check Button
{
     DeviceFrame = new QFrame(frame2);
     DeviceFrame->setGeometry(160,0,550,452);
     DeviceFrame->setStyleSheet("background-color:rgb(255, 255, 255);");
     DeviceFrame->hide();
     connect(button11,SIGNAL(clicked()),this,SLOT(DeviceFrame_show()));
}
我想在构造函数中调用ButtonChange(),就可以实现同样功能。
我现在在构造函数调用ButtonChange(),会报错。我该如何做?
离线realfan

只看该作者 16楼 发表于: 2015-11-12
回 liuligang88 的帖子
liuligang88:哦,谢谢了,真的非常感谢这么耐心的讲解。。。
我还想问一下Qt里怎么调用子函数?好比如下
void Widget::ButtonChange()//Check Button
{
....... (2015-11-11 16:08) 

怀疑问题出在frame2或button11上,你检查一下有没有犯之前的错误,局部变量与成员变量。搞不定,就把头文件, 以及构造函数贴上来我看一下。
离线liuligang88

只看该作者 17楼 发表于: 2015-11-12
回 realfan 的帖子
realfan:怀疑问题出在frame2或button11上,你检查一下有没有犯之前的错误,局部变量与成员变量。搞不定,就把头文件, 以及构造函数贴上来我看一下。 (2015-11-12 08:49) 

恩,非常感谢!!!是Button11的问题,还是之前那样的问题,已解决了。再次对你这么耐心,这么认真的讲解说声@realfan谢谢你。
快速回复
限100 字节
 
上一个 下一个