【问题标题】:Not mirroring layout direction in some of Qt widgets在某些 Qt 小部件中不镜像布局方向
【发布时间】:2019-12-23 08:48:29
【问题描述】:

我有一个应具有从右到左布局方向的应用程序。但是有一些小部件(例如 QComboBox 和 QlistWidget)我不想镜像布局方向(无论应用程序的布局方向是什么,它们都应该具有从左到右的布局方向)。 我正在寻找类似 LayoutMirroring.enabled 在 qml 中的东西。 有解决办法吗?

编辑:

这是我的代码的一个非常简化的版本: 文件小部件.h:

#include <QWidget>
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
};

文件小部件.cpp:

Widget::Widget(QWidget *parent): QWidget(parent){
setMinimumSize(300, 300);
QLabel *label1 = new QLabel("Right to left 1");
QLabel *label2 = new QLabel("Right to left 2");
QLabel *label3 = new QLabel("Right to left 3");
QComboBox *mCombo = new QComboBox();
mCombo->setMinimumWidth(150);
mCombo->addItems(QStringList({"Left to Right 1", "Left to Right 2", "Left to Right 3"}));
mCombo->setStyleSheet("QComboBox{padding: 0 10 0 10;}");
mCombo->setLayoutDirection(Qt::LeftToRight);

QVBoxLayout *mainlayout = new QVBoxLayout();
mainlayout->setAlignment(Qt::AlignLeft);

mainlayout->addWidget(mCombo);
mainlayout->addWidget(label1);
mainlayout->addWidget(label2);
mainlayout->addWidget(label3);

setLayout(mainlayout);}

这是我的 main.cpp:

#include "widget.h"
#include <QApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    a.setLayoutDirection(Qt::RightToLeft);

    Widget w;
    w.show();

    qDebug()<<a.layoutDirection() <<w.layoutDirection();

    return a.exec();
}

评论:我的项目使用了一个样式表文件,在为 QComboBox 使用了不同的样式部分之后,我意识到样式“QComboBox{padding: 0 10 0 10;}”是导致问题的原因。所以我把它包括在这里。如果我删除该行,问题将得到解决。

注意:我也意识到theWidget-&gt;setLayoutDirection(Qt::LeftToRight); 会做我正在寻找的事情,尽管我不知道这是否正确!

【问题讨论】:

  • 你知道这个文档吗? Text AlignmentLayout Mirroring?似乎这正是您正在寻找的。 (或者我误解了什么?我必须承认我不是 QML 专家。);-)
  • 是的。我知道如何在 QML 中处理布局方向。我的问题是小部件。即使应用程序的布局方向是从右到左,我也有想要从左到右的小部件。例如 QComboBox。我可以通过在我的 cpp 文件中为该组合框调用 setlayoutdirection 来强制它从左到右。但所选项目仍然遵循应用程序的布局!
  • 已添加。感谢您对最小示例的建议,我能够找到问题的根源。我还添加了我发现的解决方法。
  • 如果您找到了解决方法 - 自行回答如何?

标签: qt layout qt5


【解决方案1】:

所以,问题出在我的应用正在使用的样式表上。这行样式表“QComboBox{padding: 0 10 0 10;}”是问题的原因。我删除了它并解决了问题。虽然我不知道原因。

此外,对于不应获取应用程序布局方向的特定小部件,必须显式设置布局方向。喜欢:theWidget-&gt;setLayoutDirection(Qt::LeftToRight);

我从Qt documentaion意识到这一点

【讨论】:

    猜你喜欢
    • 2013-02-12
    • 1970-01-01
    • 2014-12-17
    • 2020-05-05
    • 1970-01-01
    • 2017-10-13
    • 2014-08-12
    • 1970-01-01
    • 2013-08-09
    相关资源
    最近更新 更多