17

QWidgetとQTabWidgetを1つの水平スプリッターに並べて配置しています。そして、スプリッターはその形を失い、マウスをその上に置くだけでスプリッターがあることを知ることができます。それを見えるようにする方法は?

ありがとう。

4

6 に答える 6

29

QSplitterHandle (ほとんどの人が「スプリッター」と考えているもの) は QWidget から派生しているため、他のウィジェットをそれに追加できます。過去にこの正確な問題を解決するために私が行ったことは次のとおりです。

// Now add the line to the splitter handle
// Note: index 0 handle is always hidden, index 1 is between the two widgets
QSplitterHandle *handle = pSplitter->handle(1);
QVBoxLayout *layout = new QVBoxLayout(handle);
layout->setSpacing(0);
layout->setMargin(0);

QFrame *line = new QFrame(handle);
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
layout->addWidget(line);

これにより、スプリッター ハンドルにくぼんだ線が追加されます。もちろん、フレームに別のスタイルを選択しlineたり、スプリッター ハンドルに追加するウィジェットとしてまったく異なるものを使用したりすることもできます。

于 2010-04-01T21:56:02.780 に答える
10

これは上記のコードに基づいていますが、両方のスプリッターの向きを処理します。私は、不透明でないサイズ変更と折りたたみ不可能な子を好んだだけです。グリップは 3 本の平行線で構成されています。ハンドルの幅で遊ぶことができますが、Windows では 7 のグリップが良さそうです。Linux または Mac でチェックインしていません。

void helper::decorateSplitter(QSplitter* splitter, int index)
{
    Q_ASSERT(splitter != NULL);

    const int gripLength = 15; 
    const int gripWidth = 1;
    const int grips = 3;

    splitter->setOpaqueResize(false);
    splitter->setChildrenCollapsible(false);

    splitter->setHandleWidth(7);
    QSplitterHandle* handle = splitter->handle(index);
    Qt::Orientation orientation = splitter->orientation();
    QHBoxLayout* layout = new QHBoxLayout(handle);
    layout->setSpacing(0);
    layout->setMargin(0);

    if (orientation == Qt::Horizontal)
    {
        for (int i=0;i<grips;++i)
        {
            QFrame* line = new QFrame(handle);
            line->setMinimumSize(gripWidth, gripLength);
            line->setMaximumSize(gripWidth, gripLength);
            line->setFrameShape(QFrame::StyledPanel);
            layout->addWidget(line);
        }
    }
    else
    {
        //this will center the vertical grip
        //add a horizontal spacer
        layout->addStretch();
        //create the vertical grip
        QVBoxLayout* vbox = new QVBoxLayout;
        for (int i=0;i<grips;++i)
        {
            QFrame* line = new QFrame(handle);
            line->setMinimumSize(gripLength, gripWidth);
            line->setMaximumSize(gripLength, gripWidth);
            line->setFrameShape(QFrame::StyledPanel);
            vbox->addWidget(line);
        }
        layout->addLayout(vbox);
        //add another horizontal spacer
        layout->addStretch();
    }
}
于 2012-11-22T13:21:54.270 に答える
8

これは、少なくともWinXPとデフォルトのLunaテーマを使用するすべてのスプリッターに当てはまります(クラシックに変更すると問題が解決します)。Lunaを使い続けたい場合は、ハンドルの背景色を変更するなどして、スプリッターのレンダリング方法を変更できます。

int main(int argc, char *argv[])    {

    QApplication a(argc, argv);
    a.setStyleSheet("QSplitter::handle { background-color: gray }");
    MainWindow w;
    w.show();
    return a.exec();
}

Qtスタイルシートの詳細については、https://doc.qt.io/qt-5/stylesheet-reference.htmlをご覧ください。

于 2010-03-30T16:53:49.010 に答える
0

ハンドルを追加したい QSplitter で splitter_handles {} を呼び出します。

#include "splitter_handles.h"
...
QSplitter spl {};
... // widgets added to 'spl'
plitter_handles {spl}; // adding handles
...

結果:

ここに画像の説明を入力

splitter_handles.h

#ifndef SPLITTER_HANDLES_H
#define SPLITTER_HANDLES_H

#include <QLayout>
#include <QPainter>
#include <QSplitter>

class handle : public QWidget
{
    Q_OBJECT

protected:
    void paintEvent(QPaintEvent *e) {
        Q_UNUSED(e);
        QPainter painter {this};
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::Dense4Pattern);
        painter.drawRect(this->rect());
    }
};

class splitter_handles {
public:
    splitter_handles(QSplitter * spl) {
        const int width {7};
        spl->setHandleWidth(width);
        for(int h_idx {1}; h_idx < spl->count(); h_idx++) {
            auto l_handle {new handle {}};
            l_handle->setMaximumSize(width*2, width*2);

            auto layout {new QHBoxLayout {spl->handle(h_idx)}};
            layout->setSpacing(0);
            layout->setMargin(1);
            layout->addWidget(l_handle);
        }
    }
};

#endif // SPLITTER_HANDLES_H

main.c

#include <QApplication>
#include <QGroupBox>
#include <QLayout>
#include <QSplitter>

#include "splitter_handles.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    auto spl_v {new QSplitter {Qt::Vertical}};
    spl_v->addWidget(new QGroupBox {"box 1"});
    spl_v->addWidget(new QGroupBox {"box 2"});
    spl_v->addWidget(new QGroupBox {"box 3"});
    splitter_handles {spl_v}; // set handles

    auto wdg  {new QWidget {}};
    auto v_lt {new QVBoxLayout {wdg}};
    v_lt->addWidget(spl_v);
    v_lt->setMargin(0);

    auto spl_h {new QSplitter {}};
    spl_h->addWidget(wdg);
    spl_h->addWidget(new QGroupBox {"box 4"});
    spl_h->addWidget(new QGroupBox {"box 5"});
    splitter_handles {spl_h};

    auto h_lt {new QHBoxLayout {}};
    h_lt->addWidget(spl_h);

    QWidget w {};
    w.setLayout(h_lt);
    w.setGeometry(100,100,500,300);
    w.show();

    return a.exec();
}
于 2020-01-28T14:35:44.123 に答える
-1

Merulaの回答のおかげで...私はこれを試してみましたが、今では私のスプリッターが見え、邪魔にならずにとても素敵に見えます. このコードは、PyQt または PySide を使用する Python 用です。

app = QtGui.QApplication(sys.argv)
app.setStyle("Plastique")   # set style (using this style shows splitters! :)
于 2014-06-24T16:49:18.590 に答える