Qt对话框大小政策

时间:2015-05-09 02:02:25

标签: c++ qt qdialog qsizepolicy

我试图在我创建的自定义QDialog类上设置大小策略。这个想法对我来说听起来很简单,但似乎我错过了一个明显的概念或Qt框架的范例。我在下面创建了一个LineDialog类,其中包含5个QRadioButtons。它们全部包含在额外级别的布局嵌套中的原因是我将很快添加其他组按钮,假设我可以使用一个。我想要的是对话框在打开时保持最小尺寸,同时仍显示所有单选按钮。我目前收到的输出是一个对话框,大小与整个计算机屏幕相同,有5个单选按钮,中间有相等数量的垂直空间,右侧有大量的填充。

我尝试在调用类的setSizePolicy(QSizePolicy::Minumum)指针和LineDialog的所有内容上调用line_dialog,如注释掉的行所示。但是,我收到有关SetSizePoliy如何成为私有函数的编译器错误,并且无法像我正在调用那样调用。这样做的正确方法是什么?

(我没有通过并正确释放我的堆内存所以请忽略它)

linedialog.cpp

#include "linedialog.h"

LineDialog::LineDialog(QWidget * parent): QDialog(parent)
{
    QGridLayout *main_layout = new QGridLayout();
    QGroupBox *line_style_group = createLineStyleGroup();
    main_layout->addWidget(line_style_group, 0, 0);
    setLayout(main_layout);
}

QGroupBox *LineDialog::createLineStyleGroup() {
    QGroupBox *line_style_box = new QGroupBox(this);
    QButtonGroup *line_style_group = new QButtonGroup(this);

    QRadioButton *solid_line = new QRadioButton("Solid line", this);
    QRadioButton *dash_line = new QRadioButton("Dash line", this);
    QRadioButton *dot_line = new QRadioButton("Dot line", this);
    QRadioButton *dash_dot_line = new QRadioButton("Dash dot line", this);
    QRadioButton *dash_dot_dot_line = new QRadioButton("Dash dot dot line", this);

    //solid_line->setSizePolicy(QSizePolicy::Minimum);
    //dash_line->setSizePolicy(QSizePolicy::Minimum);
    //dot_line->setSizePolicy(QSizePolicy::Minimum);
    //dash_dot_line->setSizePolicy(QSizePolicy::Minimum);
    //dash_dot_dot_line->setSizePolicy(QSizePolicy::Minimum);

    /** logical line style button grouping */
    line_style_group->addButton(solid_line);
    line_style_group->addButton(dash_line);
    line_style_group->addButton(dot_line);
    line_style_group->addButton(dash_dot_line);
    line_style_group->addButton(dash_dot_dot_line);

    QVBoxLayout *line_style_box_layout = new QVBoxLayout();
    line_style_box_layout->addWidget(solid_line);
    line_style_box_layout->addWidget(dash_line);
    line_style_box_layout->addWidget(dot_line);
    line_style_box_layout->addWidget(dash_dot_line);
    line_style_box_layout->addWidget(dash_dot_dot_line);

    line_style_box->setLayout(line_style_box_layout);
    return line_style_box;
}

致电课程

. . .
LineDialog *line_dialog = new LineDialog(this);
line_dialog.show();
. . .

0 个答案:

没有答案