Button 使Qt按钮相互连接

Button 使Qt按钮相互连接,button,qt4,Button,Qt4,我想通过Qt4库来实现这个效果:一个按钮组,它连接它旁边的每个按钮。在Gtk3中,我只是将“linked”类应用于容器小部件。如何在Qt4中做到这一点 在Qt中,要创建一组链接按钮,可以使用带有可检查按钮的 注意,这个类不是一个可视化的容器,您需要使用普通的布局技术自己布置按钮(或其他任何东西)。 (另一种选择是具有视觉外观的。) 使用组框并实现此效果相对简单 #include <QtGui> // For Qt 5: // #include <QtWidgets>

我想通过Qt4库来实现这个效果:一个按钮组,它连接它旁边的每个按钮。在Gtk3中,我只是将“linked”类应用于容器小部件。如何在Qt4中做到这一点


在Qt中,要创建一组链接按钮,可以使用带有可检查按钮的

注意,这个类不是一个可视化的容器,您需要使用普通的布局技术自己布置按钮(或其他任何东西)。 (另一种选择是具有视觉外观的。)

使用组框并实现此效果相对简单

#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;"
"}");
这看起来像下面的屏幕截图(取决于您的操作系统和您可能有的任何Qt定制):

  • Linux,没有任何样式:

  • 样式表如上面的代码所示:


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

谢谢,但我想要的是,在行为成瘾中,样式:按钮彼此物理连接:)
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;
        }
};