gpt4 book ai didi

java - QPushButton 删除空格 (占碑)

转载 作者:行者123 更新时间:2023-11-30 07:26:35 25 4
gpt4 key购买 nike

如何删除 QPushButton 的间隙/空间?

我正在查看 Windows 10 计算器,它是:

enter image description here

正如你所看到的,按钮 7 和 8 之间没有空格,我尝试在占碑中做同样的事情,通过设置 QGridLayoutQPushButton 的空间像这样:

QVBoxLayout main_layout = new QVBoxLayout();

QGridLayout sample_layout = new QGridLayout();
sample_layout.setHorizontalSpacing(0);
sample_layout.setVerticalSpacing(0);
sample_layout.setContentsMargins(0,0,0,0);
sample_layout.setSpacing(0);
sample_layout.setWidgetSpacing(0);

main_layout.addLayout(sample_layout);

QLineEdit edit_input = new QLineEdit();

QPushButton action_0 = new QPushButton("0");
action_0.setMinimumHeight(60);
action_0.setContentsMargins(0, 0, 0, 0);

QPushButton action_1 = new QPushButton("1");
action_1.setMinimumHeight(60);
action_1.setContentsMargins(0, 0, 0, 0);

QPushButton action_2 = new QPushButton("2");
action_2.setMinimumHeight(60);
action_2.setContentsMargins(0, 0, 0, 0);

QPushButton action_3 = new QPushButton("3");
action_3.setMinimumHeight(60);
action_3.setContentsMargins(0, 0, 0, 0);

QPushButton action_4 = new QPushButton("4");
action_4.setMinimumHeight(60);
action_4.setContentsMargins(0, 0, 0, 0);

QPushButton action_5 = new QPushButton("5");
action_5.setMinimumHeight(60);
action_5.setContentsMargins(0, 0, 0, 0);

QPushButton action_6 = new QPushButton("6");
action_6.setMinimumHeight(60);
action_6.setContentsMargins(0, 0, 0, 0);

QPushButton action_7 = new QPushButton("7");
action_7.setMinimumHeight(60);
action_7.setContentsMargins(0, 0, 0, 0);

QPushButton action_8 = new QPushButton("8");
action_8.setMinimumHeight(60);
action_8.setContentsMargins(0, 0, 0, 0);

QPushButton action_9 = new QPushButton("9");
action_9.setMinimumHeight(60);
action_9.setContentsMargins(0, 0, 0, 0);

QPushButton action_plus = new QPushButton("+");
action_plus.setMinimumHeight(120);
action_plus.setContentsMargins(0, 0, 0, 0);

QPushButton action_minus = new QPushButton("-");
action_minus.setMinimumHeight(60);
action_minus.setContentsMargins(0, 0, 0, 0);

QPushButton action_divide = new QPushButton("/");
action_divide.setMinimumHeight(60);
action_divide.setContentsMargins(0, 0, 0, 0);

QPushButton action_times = new QPushButton("X");
action_times.setMinimumHeight(60);
action_times.setContentsMargins(0, 0, 0, 0);

QPushButton action_equal = new QPushButton("=");
action_equal.setMinimumHeight(120);
action_equal.setContentsMargins(0, 0, 0, 0);

QPushButton action_dot = new QPushButton(".");
action_dot.setMinimumHeight(60);
action_dot.setContentsMargins(0, 0, 0, 0);

QPushButton action_del = new QPushButton("< Delete");
action_del.setMinimumHeight(60);
action_del.setContentsMargins(0, 0, 0, 0);

QPushButton action_clear = new QPushButton("Clear");
action_clear.setMinimumHeight(60);
action_clear.setContentsMargins(0, 0, 0, 0);

sample_layout.addWidget(edit_input,0,0,1,4);
sample_layout.addWidget(action_minus,1,0);
sample_layout.addWidget(action_divide,1,1);
sample_layout.addWidget(action_times,1,2);
sample_layout.addWidget(action_del,1,3);
sample_layout.addWidget(action_7,2,0);
sample_layout.addWidget(action_8,2,1);
sample_layout.addWidget(action_9,2,2);
sample_layout.addWidget(action_4,3,0);
sample_layout.addWidget(action_5,3,1);
sample_layout.addWidget(action_6,3,2);
sample_layout.addWidget(action_1,4,0);
sample_layout.addWidget(action_2,4,1);
sample_layout.addWidget(action_3,4,2);
sample_layout.addWidget(action_clear,5,0);
sample_layout.addWidget(action_0,5,1);
sample_layout.addWidget(action_dot,5,2);
sample_layout.addWidget(action_plus,2,3,2,1);
sample_layout.addWidget(action_equal,4,3,2,1);

但我仍然有差距。

enter image description here

注意:如果我设置 main_layout.setContentsMargins(0,0,0,0)main_layout.setWidgetSpacing(0) 我就非常适合 QLineEdit 如下图所示,但不是 QPushButton 所以我相信 QPushButton 中有一个选项会造成这个间隙,但只有 QPushButton 的选项setContentsMargins(),知道吗?

enter image description here

我确实调查了其他提出同样问题的人,他们使用QSpacerItem,该解决方案没有意义(感觉不像正确/专业的方式),原因在QLineEdit 它完美地没有任何间隙,但在 QPushButton 中则不然。

编辑:当我应用action_plus.setFlat(true);时,按钮看起来像这样:

enter image description here

最佳答案

为了避免大量编写,创建一个继承自QPushButton的自定义类。在构造函数内添加一个调用 setFlat(true) ,用于处理按钮边框的提升(除非被样式覆盖)。下面的样式表(使用 setStyleSheet(...) 设置按钮以使用它)提供了很好的悬停效果:

QPushButton:hover{
background-color: rgb(164, 156, 156);
border: 0px;
}

根据自己的喜好更改颜色以适应 Windows 10 主题。您可以省略 setFlat(true) 并添加

QPushButton{
border: 0px
}

到您的样式表(以及悬停处理程序)。效果是一样的。

结果是(在我的例子中是在 Linux 上):

enter image description here

您还可以使用样式表中的 border 属性来展平 QLineEdit:

完全没有边框:

enter image description here

1px实线边框:

enter image description here

关于java - QPushButton 删除空格 (占碑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com