button - 使 Qt 按钮相互连接

标签 button qt4

我想用 Qt4 库实现这种效果:一个按钮组,它附加了它旁边的每个按钮。在 Gtk3 中,我只是将“链接”类应用于容器小部件。这在 Qt4 中如何完成?

enter image description here

最佳答案

在 Qt 中,要创建一组链接按钮,您可以使用 QButtonGroup里面有可检查的按钮。

请注意,此类不是可视容器,您需要使用常规布局技术自行布置按钮(或其他任何东西)。 (另一种选择是 QGroupBox ,它确实具有视觉外观。)

使用分组框和style sheets 相对简单达到这个效果。

#include <QtGui>

// For Qt 5:
// #include <QtWidgets>

static QString strip_normal(
"QPushButton {"
"   margin: 0; padding: 10px; border: 0px;"
"   background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
"                              stop: 0 #f6f7fa, stop: 1 #aaabae);"
"}");
static QString strip_checked(
"QPushButton:checked {"
"   background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
"                              stop: 0 #aaabae, stop: 1 #f6f7fa);"
"}");
static QString strip_first(
"QPushButton{"
"   border-top-left-radius: 6px;"
"   border-bottom-left-radius: 6px;"
"}");
static QString strip_last(
"QPushButton{"
"   border-top-right-radius: 6px;"
"   border-bottom-right-radius: 6px;"
"}");

static QString widget_back(
"QWidget {"
"   background: black;"
"}");

渐变取自 QPushButton styling examples , 轻微调整。

class W: public QWidget
{
    Q_OBJECT
    public:
        W(QWidget *parent = 0)
            : QWidget(parent)
        {
            /* style sheet applies to this widget and its children */
            setStyleSheet(widget_back+strip_normal+strip_checked);

            /* First and last widget need special borders */
            QPushButton *one = createButton("one",   true,  strip_first);
            QPushButton *two = createButton("two",   false);
            QPushButton *thr = createButton("three", false, strip_last);

            /* Button group for button selection handling */
            QButtonGroup *bg = new QButtonGroup;
            bg->addButton(one);
            bg->addButton(two);
            bg->addButton(thr);

            /* Layout with no spacing */
            QHBoxLayout *hl = new QHBoxLayout;
            hl->addWidget(one);
            hl->addWidget(two);
            hl->addWidget(thr);
            hl->setSpacing(0);

            setLayout(hl);
        }

        QPushButton *createButton(QString const& name,
                                  bool checked,
                                  QString const& sheet = QString())
        {
            QPushButton *pb = new QPushButton(name);
            pb->setCheckable(true);
            pb->setChecked(checked);
            if (!sheet.isEmpty())
                pb->setStyleSheet(sheet);
            return pb;
        }
};

这将类似于下面的屏幕截图(取决于您的操作系统和您可能拥有的任何 Qt 定制):

  • Linux,没有任何样式:

    enter image description here

  • 上面代码中的样式表:

    enter image description here

其他可能对您有帮助的选择,具体取决于“控制”对您具有的语义:QTabBar这是选项卡式窗口(选项卡选择器)中的顶部小部件。

关于button - 使 Qt 按钮相互连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16264728/

相关文章:

Qt QGraphicsView 中心的一张背景图像

javascript - QML 中 default 关键字的用途是什么?

c++ - 一种在 Qt 中管理 GUI 状态的 RAII

Android:具有多个可点击按钮的 ListView 元素

javascript - 如何让这个按钮只调用该函数一次?

javascript - 我怎样才能改变我的按钮而不是将div从可见更改为不可见,而是做相反的事情?

javascript - 按下按钮时清除段落文本

Java-热衷于只有 1 个按钮来来回切换,而不是两个按钮?

c++ - QScintilla - 在自定义词法分析器中为单词添加颜色

macos - QFileDialog:如何在 "Save as..."对话框中设置默认文件名