在鼠标悬停期间,QPushButton和QToolButton的样式相同

在鼠标悬停期间,QPushButton和QToolButton的样式相同,qt,hover,styles,qpushbutton,qtoolbutton,Qt,Hover,Styles,Qpushbutton,Qtoolbutton,是否可以使QPushButton和QToolButton在鼠标悬停期间看起来相同,即悬停事件的外观 我不使用自定义样式表,但有一个全局应用程序设置: QtGui.QApplication.setStyle("plastique") 我正在寻找类似“传播”QPushButton的样式表上的当前(系统默认)鼠标到QToolButton的东西。默认情况下,将鼠标悬停在QPushButton上时,QPushButton将高亮显示,而QToolButton则什么也不做 环境:Qt 4.8.6,在Linu

是否可以使QPushButton和QToolButton在鼠标悬停期间看起来相同,即悬停事件的外观

我不使用自定义样式表,但有一个全局应用程序设置:

QtGui.QApplication.setStyle("plastique")
我正在寻找类似“传播”QPushButton的样式表上的当前(系统默认)鼠标到QToolButton的东西。默认情况下,将鼠标悬停在QPushButton上时,QPushButton将高亮显示,而QToolButton则什么也不做


环境:Qt 4.8.6,在Linux CentOS 6和Windows 7上运行

以Nicolas answer为起点,我创建了自己的CSS样式表:

QPushButton,QToolButton {

  background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #fdfbf7, stop: 1 #cfccc7);
  border-width: 1px;
  border-color: #8f8f91;
  border-style: solid;
  border-radius: 3px;
  padding: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

QToolButton[popupMode="1"] {

  padding-right: 18px;
}

QToolButton::menu-button {

  border-width: 1px;
  border-color: #8f8f91;
  border-style: solid;
  border-top-right-radius: 3px;
  border-bottom-right-radius: 3px;
  /* 16px width + 2 * 1px for border = 18px allocated above */
  width: 16px;
}

QPushButton:hover,QToolButton:hover {

  border-top-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #b2afaa, stop: 0.5 #4847a1, stop: 1 #7e7cb6);
  border-radius: 1px;
  border-top-width: 3px;
  border-bottom-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #7e7cb6, stop: 0.5 #4847a1, stop: 1 #b2afaa);
  border-bottom-width: 3px;
  background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #e4e0e1, stop: 1 #cfcbcd);
}

QPushButton:pressed,QToolButton:pressed {

  background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #cfcbcd, stop: 1 #e4e0e1);
  border-width: 1px;
  border-color: #8f8f91;
}

QPushButton:focus,QToolButton:focus {

  outline: none;
}
为了应用该样式,我使用了以下PyQT代码段:

cssFile = os.path.join(self.installDir, "etc", "Buttons.css")
with open(cssFile, "r") as fh:
    cssStyleSheet = "".join(fh.readlines())

    for i in range(self.verticalLayout.count()):
        widget = self.verticalLayout.itemAt(i).widget()
        if isinstance(widget, QtGui.QPushButton) or isinstance(widget, QtGui.QToolButton):
            widget.setStyleSheet(cssStyleSheet)
        # Make Tool- and PushButton same height...
        if isinstance(widget, QtGui.QToolButton):
            widget.setMaximumHeight(self.firstPushButton.sizeHint().height())
如果应用程序背景色设置为“#ede9e3”,则生成的外观几乎与默认的“plastique”样式相同


首先,我将样式表应用于所有工具和按钮(即系统默认设置)。但在此之后,对话框中的所有按钮都丢失了默认宽度。因此,我决定将该样式仅应用于垂直布局内部的按钮,其中包含所有涉及的按钮。

因此,基本上你是说你只想在QPushButton上禁用悬停效果,这实际上与QToolButton无关,对吗?不,我想在QToolButton上启用悬停效果(看起来像QPushButton)。最简单的方法是创建自定义样式表,为两个按钮提供相似的样式。系统风格的行为倾向于被烘焙,当它们没有API调用时不容易被操纵。