• 5293阅读
  • 2回复

[已解决]新建一个搜索窗口,setLayout出现问题!!! [复制链接]

上一主题 下一主题
离线bingogo
 
只看楼主 倒序阅读 楼主  发表于: 2009-05-25
新建一个搜索窗口,setLayout出现问题!!!
本人是菜鸟,如果有什么建议和意见,请指出,万分感激!!

// AutoSearchDlg.h
class AutoSearchDlg : public QDialog
{
     Q_OBJECT

public:
     AutoSearchDlg(QWidget *parent = 0);
public slots:
     void SetupDialog();

private:
     QLabel *textLabel;
     QLabel *ipLabel;
     QLineEdit *ipLineEdit;
     QDialogButtonBox *buttonBox;
     QPushButton *findButton;

QGridLayout *buttomLayout;
QVBoxLayout *mainLayout;

};


// AutoSearchDlg.cpp
AutoSearchDlg::AutoSearchDlg(QWidget *parent)
     : QDialog(parent)
{

}

void AutoSearchDlg::SetupDialog()
{

     textLabel = new QLabel(tr("Please input IP address and mask for network search.\n"));

     ipLabel = new QLabel(tr("IP Address:"));
     ipLineEdit = new QLineEdit;
     ipLabel->setBuddy(ipLineEdit);

     findButton = new QPushButton(tr("&Find"));
     findButton->setDefault(true);


     buttomLayout = new QGridLayout;
     buttomLayout->addWidget(ipLabel, 0, 0);
     buttomLayout->addWidget(ipLineEdit, 0, 1);
     buttomLayout->addWidget(buttonBox,2,2);

     mainLayout = new QVBoxLayout;
     mainLayout->addWidget(textLabel);
     mainLayout->addLayout(buttomLayout);

     setLayout(mainLayout);
    
     exec();

}



// mainwindwo.cpp
#include "AutoSearchDlg.h"

MainWindow::MainWindow()
{
// 创建菜单工具栏等操作 ……

// 触发的Action
    AutoSearchDlg *autoSearchDlg = new AutoSearchDlg();  
  
    AutoSearchAct = new QAction(tr("Auto Search"), this);
    connect(AutoSearchAct, SIGNAL(triggered()), autoSearchDlg, SLOT(SetupDialog()));

}


运行,点击按钮触发时候,第一次是正常的
关闭搜索窗口后,从主窗口再次触发
就出现以下问题
QWidget::setLayout: Attempting to set QLayout "" on AutoSearchDlg "", which already has a layout

有知道原因的吗?谢谢
[ 此帖被bingogo在2009-05-25 15:41重新编辑 ]
离线bingogo
只看该作者 1楼 发表于: 2009-05-25
GOOGLE之后,没有发现有用的信息……
离线bingogo
只看该作者 2楼 发表于: 2009-05-25
解决了:


Export the creation of your gui prts into the constructor:
AutoSearchDlg::AutoSearchDlg(QWidget *parent)
>      : QDialog(parent)
> {
> textLabel = new QLabel(tr("Please input IP address and mask for
> network search.\n"));
>
>      ipLabel = new QLabel(tr("IP Address:"));
>      ipLineEdit = new QLineEdit;
>      ipLabel->setBuddy(ipLineEdit);
>
>      findButton = new QPushButton(tr("&Find"));
>      findButton->setDefault(true);
>
>
>      buttomLayout = new QGridLayout;
>      buttomLayout->addWidget(ipLabel, 0, 0);
>      buttomLayout->addWidget(ipLineEdit, 0, 1);
>      buttomLayout->addWidget(buttonBox,2,2);
>
>      mainLayout = new QVBoxLayout;
>      mainLayout->addWidget(textLabel);
>      mainLayout->addLayout(buttomLayout);
>
>      setLayout(mainLayout);
> }
>
> void AutoSearchDlg::SetupDialog()
> {
>
>
>
>      exec();
>
> }


With every call of AutoSearchDlg::SetupDialog(), you are reinitialzing all the objects of your dialog (without deleting the old ones first).
This is also a severe memory leak.
快速回复
限100 字节
 
上一个 下一个