Qt Gui 第六章绘图类

1、QRadioButton之间如何互斥

  其中一种方法是将各个QRadioButton控件放在同一个toolbarsLayout或者toolbarsGroupBox即可;如下所示

    toolbarsGroupBox = new QGroupBox(tr("Show toolbars as"));
    picturesAndTextRadioButton = new QRadioButton(tr("Pictures and text"));
    picturesOnlyRadioButton = new QRadioButton(tr("Pictures only"));
    textOnlyRadioButton = new QRadioButton(tr("Text only"));

    QVBoxLayout* toolbarsLayout = new QVBoxLayout;
    toolbarsLayout->addWidget(picturesAndTextRadioButton);
    toolbarsLayout->addWidget(picturesOnlyRadioButton);
    toolbarsLayout->addWidget(textOnlyRadioButton);
    toolbarsGroupBox->setLayout(toolbarsLayout);

  如上图所示:将picturesAndTextRadioButton、picturesOnlyRadioButton和textOnlyRadioButton放在同一个toolbarsLayout里面;即会产生互斥;

  

  总结:控件间的互斥即将QRadioButton放在同一个父窗口,则这些同一个父窗口的控件(layout窗口也算)即产生互斥;

     也可以通过构造函数中直接指定父窗口:QRadioButton(const QString &text, QWidget *parent = Q_NULLPTR)、QRadioButton(QWidget *parent = Q_NULLPTR)

     信号连接:当radiobutton按钮被点击的时候会触发两个信号,toggled(bool)和clicked();咱们一般是用toggled(bool),因为该信号会传入radiobutton的状态是选中还是不选中。

2、QGridLayout

  QVBoxLayout、QHBoxLayout和QGridLayout都直接或间接继承QLayout;

QGridLayout即网格的布局形式;比QVBoxLayout和QHBoxLayout的使用更灵活但是使用过程中也是更复杂;需要指定竖排和横排的顺序;还有占用的位置个数等;

    QGridLayout *leftLayout = new QGridLayout;
    leftLayout->addWidget(namedLabel, 0, 0);
    leftLayout->addWidget(namedLineEdit, 0, 1);
    leftLayout->addWidget(lookInLabel, 1, 0);
    leftLayout->addWidget(lookInLineEdit, 1, 1);
    leftLayout->addWidget(subfoldersCheckBox, 2, 0, 1, 2);
    leftLayout->addWidget(tableWidget, 3, 0, 1, 2);
    leftLayout->addWidget(messageLabel, 4, 0, 1, 2);

  QGridLayout的addWidget

    inline void addWidget(QWidget *w) { QLayout::addWidget(w); }
    void addWidget(QWidget *, int row, int column, Qt::Alignment = Qt::Alignment());
    void addWidget(QWidget *, int row, int column, int rowSpan, int columnSpan, Qt::Alignment = Qt::Alignment());
    void addLayout(QLayout *, int row, int column, Qt::Alignment = Qt::Alignment());
    void addLayout(QLayout *, int row, int column, int rowSpan, int columnSpan, Qt::Alignment = Qt::Alignment());

  如上rowSpan和columnSpan是指要占用的行和竖的位置个数;默认是1;

  如row和column是指定在这个QGridLayout布局下,要排在第几行第几列;


3、QStackedLayout

  该函数也是继承QLayout;即可以在该页面放置多个窗口。但是该控件的特性是,一次只能显示一个窗口。

  a stack of widgets where only one widget is visible at a time。

    stackedLayout = new QStackedLayout;
    stackedLayout->addWidget(appearancePage);
    stackedLayout->addWidget(webBrowserPage);
    stackedLayout->addWidget(mailAndNewsPage);
    stackedLayout->addWidget(advancedPage);

  如上所示,只能显示其中一个页面;其他页面隐藏状态;但是可以通过setCurrentIndex(int)函数来指定当前要显示的是哪个页面;

  但是特殊情况下,可以显示所有的页面;通过:stackedLayout->setStackingMode(QStackedLayout::StackAll); 但是除了设置的currentindex外,其他的页面都是disable的状态;

猜你喜欢

转载自www.cnblogs.com/czwlinux/p/12306741.html