QWidgets在show()之后没有显示 - c ++ / Qt5

时间:2017-12-30 15:47:21

标签: c++ windows qt user-interface

我目前正在使用Qt5开发数据库管理软件。我想隐藏一些链接到程序主要布局的小部件,然后再次显示它们(使用QComboBox选择是否显示)。所有小部件都是MainWindow类的成员。 这就是我创建小部件的方式:

void MainWindow::mode1Buildup()
{
    extractTables();
    mod1TableDesc = new QLabel("Cette requête a pour but \n de filtrer la table client \n avec ce paramètre :");
    mainLayout->addWidget(mod1TableDesc,3,0,1,1,Qt::AlignLeft);
    QSqlQuery query;
    if(query.exec("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_SCHEMA='auchan' AND TABLE_NAME='approvisionnement')"))
    {
        while(query.next())
        {
            fieldNames.push_back(q2c(query.value(0).toString()));
        }
        createFieldBox();
    }
    else
    {
        QMessageBox::about(this, "Tables non récupérées", "La récupération des tables a échouée");
    }
    exactBox = new QComboBox;
    exactBox->addItem("Contient");
    exactBox->addItem("Commence par");
    exactBox->addItem("fini par");
    exactBox->addItem("Condition");
    mainLayout->addWidget(exactBox, 5,0,1,1,Qt::AlignLeft);
    filter1 = new QComboBox;
    filter1->addItem("=");
    filter1->addItem("<=");
    filter1->addItem(">=");
    filter1->addItem("<");
    filter1->addItem(">");
    mainLayout->addWidget(filter1,6,0,1,1,Qt::AlignLeft);
    value = new QLineEdit();
    mainLayout->addWidget(value,7,0,1,1,Qt::AlignLeft);
    value->setObjectName("test1");
    borderBox = new QCheckBox("Cochez pour avoir une valeur \n comprise dans un ensemble", this);
    connect(borderBox, SIGNAL(clicked(bool)), this, SLOT(makeInter()));
    mainLayout->addWidget(borderBox, 8,0,1,1,Qt::AlignLeft);
    lowvalueDesc = new QLabel("Entrez la valeur minimum");
    mainLayout->addWidget(lowvalueDesc, 9,0,1,1,Qt::AlignLeft);
    lowvalueDesc->setVisible(false);
    lowvalue = new QLineEdit();
    mainLayout->addWidget(lowvalue, 10,0,1,1,Qt::AlignLeft);
    lowvalue->setVisible(false);
    highvalueDesc = new QLabel("Entrez la valeur maximum");
    mainLayout->addWidget(highvalueDesc, 11,0,1,1,Qt::AlignLeft);
    highvalueDesc->setVisible(false);
    highvalue = new QLineEdit();
    mainLayout->addWidget(highvalue, 12,0,1,1,Qt::AlignLeft);
    highvalue->setVisible(false);
    recherche1 = new QPushButton("recherche");
    connect(recherche1, SIGNAL(clicked(bool)), this, SLOT(execReq1()));
    mainLayout->addWidget(recherche1,13,0,1,1,Qt::AlignLeft);


}

这是我隐藏它们的方式:

void MainWindow::mode1Cleanup()
{
    mod1TableDesc->hide();
    fieldSelect->hide();
    value->hide();
    filter1->hide();
    exactBox->hide();
    borderBox->hide();
    lowvalue->hide();
    highvalue->hide();
    recherche1->hide();
    highvalueDesc->hide();
    lowvalueDesc->hide();
}

这就是我向他们展示的方式:

void MainWindow::mode1Rebuild()
{
    mod1TableDesc->show();
    fieldSelect->show();
    value->show();
    filter1->show();
    exactBox->show();
    borderBox->show();
    lowvalue->show();
    lowvalueDesc->show();
    highvalue->show();
    highvalueDesc->show();
    recherche1->show();
}

但是,我再也无法再显示它们了。它们不会从主布局中删除。我想也许他们被垃圾收集器抓住了,但我不确定如何检查/修复它。

想要完整的代码: https://github.com/Sysmetryx/ESME-PROJET-IHM-BDD/tree/V.2Debug

我也尝试过 - &gt; setVisible(true)和setVisible(false),但是等效,它什么都没改变。 我也看了看这个帖子: QWidget not showing after calling show() 并且不理解答案,主要是因为它在Python中。

感谢您的时间, 最好的祝福, 森

1 个答案:

答案 0 :(得分:0)

我真的没有看到通过代码添加小部件到布局的原因。将小部件放在UI文件中,在代码中设置它并在开始时隐藏。 可以帮助您清除代码并解决问题。

如果您不想在UI文件中创建小部件。您应该为新窗口小部件指定“父级”。 例如:

value = new QLineEdit();

是错误的做法。请使用下一个模板:

value = new QLineEdit( this );

这对你也有帮助。