gpt4 book ai didi

css - QT5 样式 QPushButton 以移除其周围的空间

转载 作者:行者123 更新时间:2023-11-27 22:51:07 24 4
gpt4 key购买 nike

我在 Qt5 Qt Widgets Application 中创建简单的滑动菜单。

一切似乎都很好,现在我的表格看起来像这样:

my test app form screenshot

当我按下按钮时子菜单显示,其他元素向下滑动。

这里是表格的代码:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>695</width>
<height>388</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QWidget" name="centralwidget">
<property name="styleSheet">
<string notr="true">QWidget#centralwidget {
border-top: 1px solid gray;
}</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="menuContainerBox" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>101</width>
<height>0</height>
</size>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="styleSheet">
<string notr="true">QWidget#menuContainerBox{

border-right: 1px solid gray;

}

QWidget#menuBoxWidget QPushButton
{
border: 1px solid gray;
border-bottom: none;
border-left: none;
border-right: none;
padding: 3 20px;
text-align: left;
}


QWidget#menuBoxWidget QWidget QPushButton
{
padding: 3 10px;
text-align: right;
}


QWidget#menuBoxWidget QWidget QPushButton:hover
{
background-color: rgb(168, 168, 168);
}

QWidget#menuBoxWidget QPushButton:hover
{
background-color: rgb(168, 168, 168);
}


QWidget#menuBoxWidget {
border-bottom: 1px solid gray;
}</string>
</property>
<layout class="QVBoxLayout" name="menuContainer">
<property name="spacing">
<number>0</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="menuBoxWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<layout class="QVBoxLayout" name="menuBox">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="menu1">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Menu1</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="subMenuBox1" native="true">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4" stretch="0,0">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="pushButton_5">
<property name="text">
<string>Submenu1_1</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>Submenu1_2</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QPushButton" name="menu2">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Menu2</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="subMenuBox2" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="pushButton_11">
<property name="text">
<string>Submenu2_1</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_7">
<property name="text">
<string>Submenu2_2</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_8">
<property name="text">
<string>Submenu2_3</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QPushButton" name="menu3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Menu3</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="subMenuBox3" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="pushButton_9">
<property name="text">
<string>Submenu3_1</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>50</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="LogoBtn">
<property name="styleSheet">
<string notr="true">border: none;
font-width: bold;</string>
</property>
<property name="text">
<string>LOGO</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>695</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuAbout">
<property name="title">
<string>About</string>
</property>
<addaction name="actionAbout"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuAbout"/>
</widget>
<action name="actionAbout">
<property name="text">
<string>About...</string>
</property>
</action>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>

and here program code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->subMenuBox1->hide();
ui->subMenuBox2->hide();
ui->subMenuBox3->hide();
}

MainWindow::~MainWindow()
{
delete ui;
}



void MainWindow::on_menu1_clicked()
{
qInfo() << "Clicked!";
if (ui->subMenuBox1->isHidden()){
ui->subMenuBox1->show();
ui->subMenuBox2->hide();
ui->subMenuBox3->hide();
}
}

void MainWindow::on_menu2_clicked()
{
qInfo() << "Clicked!";
if (ui->subMenuBox2->isHidden()){
ui->subMenuBox2->show();
ui->subMenuBox1->hide();
ui->subMenuBox3->hide();
}
}

void MainWindow::on_menu3_clicked()
{
qInfo() << "Clicked!";
if (ui->subMenuBox3->isHidden()){
ui->subMenuBox3->show();
ui->subMenuBox2->hide();
ui->subMenuBox1->hide();
}
}

它简单而且工作得很好。

但现在我需要通过删除按钮之间的所有空格并将按钮文本向左对齐以及子菜单按钮文本向右对齐来设置样式。

我尝试为 subMenuBox1 设置 padding:0 但它只影响按钮高度。

我还需要在 menuBoxWidget 的右侧添加一行,以便将菜单与其他屏幕分开。

如何设置样式,使菜单看起来不错?

更新 1:

我更新了添加了样式的表单代码。

最佳答案

好的,我知道它是如何工作的!

此处样式:

QWidget#menuContainerBox{

border-right: 1px solid gray;

}

QWidget#menuBoxWidget QPushButton
{
border: 1px solid gray;
border-bottom: none;
border-left: none;
border-right: none;
padding: 3 20px;
text-align: left;
}


QWidget#menuBoxWidget QWidget QPushButton
{
padding: 3 10px;
text-align: right;
}


QWidget#menuBoxWidget QWidget QPushButton:hover
{
background-color: rgb(168, 168, 168);
}

QWidget#menuBoxWidget QPushButton:hover
{
background-color: rgb(168, 168, 168);
}


QWidget#menuBoxWidget {
border-bottom: 1px solid gray;
}

现在看起来,它非常基本,但它可以正常工作并且看起来像我想要的!

result screnshot

关于css - QT5 样式 QPushButton 以移除其周围的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443361/

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