【问题标题】:QWidgets not showing after show() -- c++/Qt5QWidgets 在 show() 之后不显示——c++/Qt5
【发布时间】:2017-12-30 15:47:21
【问题描述】:

我目前正在使用 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

我也尝试了 ->setVisible(true) 和 setVisible(false),但是等效,它没有任何改变。 我还看了一下这个线程: QWidget not showing after calling show() 并且没看懂答案,主要是因为它是在 Python 中的。

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

【问题讨论】:

标签: c++ windows qt user-interface


【解决方案1】:

我真的看不出通过代码将小部件添加到布局的原因。将小部件放在 UI 文件中,在代码中设置它并在启动时隐藏。这可能帮助您使代码清晰并解决您的问题。

如果您不想在 UI 文件中制作小部件。您应该为新小部件分配“父级”。 例如:

value = new QLineEdit();

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

value = new QLineEdit( this );

这对你也有帮助。

【讨论】:

  • 建议将父母传给他,但这不是强制性或必要的。
  • 谢谢你关于父母的提示,我会添加它们,它可能会更干净。不幸的是,我的一位老师要求我不要使用 UI。
  • 专业提示:首先使用设计器制作,然后将生成的代码复制到项目中并删除.ui。那么你的老师就不能抱怨了!
猜你喜欢
  • 2020-03-28
  • 2020-11-05
  • 1970-01-01
  • 2015-05-01
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多