简体   繁体   English

在QT中通过qss定位QPushButtons

[英]Positioning QPushButtons via qss in QT

I have a set of five buttons that I am trying to position in a qss file. 我有一组试图放置在qss文件中的五个按钮。 The default position that I have set up in the ui file works for one of the layouts I need. 我在ui文件中设置的默认位置适用于我需要的一种布局。 However, I want to group the buttons differently in the other theme. 但是,我想在其他主题中对按钮进行不同的分组。

I am new to qss files and have been experimenting, but cannot figure out if some things are possible. 我是qss文件的新手,并且一直在尝试,但是无法弄清楚是否有可能。 The "left" property is defined here: http://doc.qt.io/archives/4.6/stylesheet.html#left-attr but nothing happens when I try to use it. 此处定义了“ left”属性: http : //doc.qt.io/archives/4.6/stylesheet.html#left-attr,但是当我尝试使用它时,什么也没有发生。

margin-left actually moves the button, but only relationally. 实际上,margin-left会移动按钮,但只能相对移动。 So, if the buttons are positioned in the ui file with a gap of 100 between them, a margin-left for the second button in the list is offset by 100. 因此,如果按钮在ui文件中的位置之间有100的间距,则列表中第二个按钮的左边距将偏移100。

What am I doing wrong? 我究竟做错了什么? Could it be some setting in the ui file that is preventing it from moving? 可能是ui文件中的某些设置阻止了它的移动? I already "broke layout" and it doesn't seem to matter. 我已经“中断了布局”,这似乎无关紧要。 Is there a good resource you'd suggest? 您会建议有很好的资源吗?

Here is a sample of my qss file. 这是我的qss文件的示例。 The left has no effect. 左侧无效。

QPushButton#Button_1 
{       
    min-width: 50;
    max-width: 50;
    min-height: 50;
    max-height: 50; 
    position:absolute;
    subcontrol-origin: border;  
    left:200;
}

EDIT: I've figured out that I can change the position of the button by deriving a class from QPushButton and making a "GeomX" qproperty. 编辑:我发现我可以通过从QPushButton派生一个类并制作一个“ GeomX” qproperty来更改按钮的位置。 This seems to work, but I am running into an odd issue now. 这似乎可行,但是我现在遇到一个奇怪的问题。 When I first load my app, it draws the buttons as they are positioned on the ui file. 首次加载应用程序时,它会绘制按钮在ui文件上的位置。 When I use the "change theme" option that I've coded, and select the currently loaded theme, it moves the buttons as I'd expect. 当我使用编码的“更改主题”选项并选择当前加载的主题时,它将按我的期望移动按钮。 However, resizing the app dumps them back to the ui positions and restarting also places them back in their ui positions. 但是,调整应用程序的大小会将它们转储回ui位置,重新启动也会将它们重新放置在ui位置。 Is there a setting in the ui file that I could alter to get it to stop moving them? ui文件中是否有设置可以更改以停止移动它们? Is there a load-order issue that I need to address? 我需要解决加载顺序问题吗? Is it possible to even address this? 有可能解决这个问题吗? What is going on? 到底是怎么回事?

Generally speaking, style sheets in Qt are used to alter the way a widget is drawn, not where it is positioned (except to add padding/margin). 一般来说,Qt中的样式表用于更改绘制小部件的方式,而不是更改其位置(添加填充/边距除外)。 As the documentation you've referenced mentions, the "left" property is specific to sub-controls (that is, components within a widget and not the widget itself). 如您所参考的文档所述,“ left”属性特定于子控件(即,控件内的组件,而不是控件本身)。

What it sounds like you're trying to accomplish (change the layout depending on the theme) would likely require a different approach. 听起来您要完成的工作(根据主题更改布局)可能需要使用不同的方法。 A couple of options would be to react to when the theme changes by: 可以通过以下几种方法来响应主题更改:

  • Moving around your spacers in your layout to move the buttons to the desired position 在布局中的间隔物周围移动以将按钮移动到所需位置
  • Using a stacked widget, one page in the stack for each layout you desire, and change which page in the stack you're showing depending on what theme you're using. 使用堆叠的小部件,可以在堆栈中为每个所需的布局分配一页,并根据要使用的主题更改要显示的堆叠页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM