【问题标题】:Qt dialog not showing its widgetsQt 对话框未显示其小部件
【发布时间】:2021-03-24 12:32:46
【问题描述】:

我想创建自定义 QT 对话框(非模态)。我的实现的问题是它只显示带有标题的对话框窗口,没有我添加到它的小部件。

下面的代码(我已经省略了大部分,只添加了对话框和主窗口部分)。

MainWindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
/// some other stuff
private:
    std::unique_ptr<ui::DialogAddUpdateItem> addItemDialog;
/// some other stuff
}

MainWindow.cpp

/// some stuff
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
/// some stuff
    addItemButton = new QPushButton(tr("Add item"));
    QObject::connect(addItemButton, &QPushButton::pressed, this, &MainWindow::openAddItemDialog);
    navLay->addWidget(addItemButton);

    addItemDialog = make_unique<ui::DialogAddUpdateItem>(this);
/// some stuff
}


void MainWindow::openAddItemDialog() {
    addItemDialog->show();
    //addItemDialog->raise(); does not work with or without those functions
    //addItemDialog->activateWindow();
    //QApplication::processEvents();
}

DialogAddUpdateItem.h

namespace ui {
class DialogAddUpdateItem : public QDialog
{
    Q_OBJECT

public:
    DialogAddUpdateItem(QWidget *parent = nullptr);

private:
    QPushButton *buttonAcc, *buttonRevert, *buttonCancel;
    QGroupBox *centralWidget, *buttonsWidget;
    QLabel *labelName, *labelDescription;
    QLineEdit *textName;
    QPlainTextEdit *textDescription;
}
}

DialogAddUpdateItem.cpp

namespace ui {
DialogAddUpdateItem::DialogAddUpdateItem(QWidget *parent) : QDialog(parent)
{
    if (!item) {
        setWindowTitle(tr("New object"));
    }

    centralWidget = new QGroupBox;

    QHBoxLayout *itemLay = new QHBoxLayout;
    centralWidget->setLayout(itemLay);

    labelName = new QLabel(tr("Name"));
    itemLay->addWidget(labelName);

    textName = new QLineEdit;
    if (item) {
        textName->setText(QString::fromStdString(item->getName()));
    }
    itemLay->addWidget(textName);

    labelDescription = new QLabel(tr("Description"));


    if (item) {
        textDescription = new QPlainTextEdit(QString::fromStdString(item->getDescription()));
    } else {
        textDescription = new QPlainTextEdit;
    }
    itemLay->addWidget(textDescription);

    buttonAcc = new QPushButton(tr("Save"));
    buttonAcc->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    QObject::connect(buttonAcc, &QPushButton::clicked, this, &DialogAddUpdateItem::acceptItem);

    buttonRevert = new QPushButton(tr("Revert"));
    buttonRevert->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    QObject::connect(buttonRevert, &QPushButton::clicked, this, &DialogAddUpdateItem::revertItem);

    buttonCancel = new QPushButton(tr("Cancel"));
    buttonCancel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    QObject::connect(buttonCancel, &QPushButton::clicked, this, &DialogAddUpdateItem::cancelItem);

    buttonsWidget = new QGroupBox;
    itemLay->addWidget(buttonsWidget);
    QVBoxLayout *buttonsLay = new QVBoxLayout;
    buttonsLay->addWidget(buttonAcc);
    buttonsLay->addWidget(buttonRevert);
    buttonsLay->addWidget(buttonCancel);
}
}

【问题讨论】:

    标签: c++ qt qdialog


    【解决方案1】:

    DialogAddUpdateItem.cpp 中,centralWidget 没有父级,因此它没有绑定到任何东西,因此不会显示。你应该修改这个:

    centralWidget = new QGroupBox;
    

    进入这个:

    centralWidget = new QGroupBox(this);
    

    现在DialogAddUpdateItem 将成为centralWidget 的父级并且应该显示它。

    另外,您似乎将一些其他小部件留下了没有父级 - 这可能会造成麻烦。比如QLineEdit textName就有这个问题。

    【讨论】:

    • 谢谢,它解决了问题。据我了解,我应该将小部件的父级设置为它正在显示的小部件。那么label parent是centralWidget,centralWidget parent是DialogAddUpdateItem等?
    • 是的,你说的很对。我现在无法分析您的代码,但我认为您明白了。您只需要记住保持各种小部件之间的这种父子关系。
    猜你喜欢
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    相关资源
    最近更新 更多