#!!/usr/bin/python
# Qt modules
from PyQt4 import QtCore, QtGui
import sys
class gui(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Grid of buttons')
self.resize(450, 300)
#------------------------------------------------------------------------------
# WIDGETS
#------------------------------------------------------------------------------
self.grid = QtGui.QGridLayout()
btnNum = 1
columnCount = 0
rowCount = 0
while btnNum != 26:
button = QtGui.QPushButton(str(btnNum))
self.grid.addWidget(button, rowCount, columnCount)
btnNum += 1
if columnCount == 4:
columnCount = 0
rowCount += 1
else:
columnCount += 1
#------------------------------------------------------------------------------
# LAYOUT
#------------------------------------------------------------------------------
self.idChannelGroup = QtGui.QGroupBox("My Button Layout")
self.idChannelGroup.setLayout(self.grid)
self.mainLayout = QtGui.QVBoxLayout()
self.mainLayout.addWidget(self.idChannelGroup) …Run Code Online (Sandbox Code Playgroud) 我会创建一个带有 3 个选项的按钮,当您做出选择时,该按钮会更改其文本。
这个解决方案对我有用:
def swTrigger(self):
self.setTrigger(self.ui.triggerButton,'Software')
def hwTrigger(self):
self.setTrigger(self.ui.triggerButton,'Hardware')
def bothTrigger(self):
self.setTrigger(self.ui.triggerButton,'Both')
def setTrigger(self,pushButton,value):
pushButton.setText(value)
#other actions
def uiConfig(self):
##triggerbutton configuration
menu = QtGui.QMenu()
menu.addAction('Software',self.swTrigger)
menu.addAction('Hardware',self.hwTrigger)
menu.addAction('Both', self.bothTrigger)
self.ui.triggerButton.setText("Software")
self.ui.triggerButton.setMenu(menu)
Run Code Online (Sandbox Code Playgroud)
但是我想避免为每个菜单项制作一个方法,因为我想制作动态菜单条目。
有一个更好的方法吗?
我想在主类中有一个函数,它不仅具有自身参数.
class Ui_Form(object):
def clearTextEdit(self, x):
self.plainTextEdit.setPlainText(" ")
print("Script in Textbox is Cleaned!",)
Run Code Online (Sandbox Code Playgroud)
x将是我的附加参数,我希望通过单击调用clearTextEdit.
self.pushButton_3.clicked.connect(self.clearTextEdit(x))
Run Code Online (Sandbox Code Playgroud)
它不允许我在点击时将x作为参数写入.你能帮助我吗!
当QPushButton被点击时,我希望它保持向下压,直到再次点击.
void MainWindow::itemClicked(){
QPushButton *clickedItem = qobject_cast<QPushButton *>(sender());
qDebug() << clickedItem->isDown();
if(!clickedItem->isDown())
clickedItem->setDown(true);
else
clickedItem->setDown(false);
}
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用.这将导致无限期按下按钮.
clickedItem->isDown() 总是假的.
Flat QPushButtons没有鼠标悬停的迹象.有没有办法为平面按钮悬停状态设置样式,以便它匹配正常按钮悬停状态(或类似的东西)的样式?

如果它意味着偏离默认外观或者必须从头开始设计按钮样式,我不想使用样式表.
我厌倦了搜索!
我从QPushbutton继承了一个Button并将我的QSS设置为它.这种风格是理想的.
所有我想要的是当按钮悬停(enterevent)按钮的颜色在特定时间(例如0.2秒)的颜色变化不是立即(软颜色变化)
我该怎么办 ?
*******在PyQt4中回答*********
class MyButton(QPushButton):
def __init__(self):
super(MyButton, self).__init__()
self.setMinimumSize(80,50)
self.setText('QPushButton')
def getColor(self):
return Qt.black
def setColor(self, color):
self.setStyleSheet("background-color: rgb({0}, {1}, {2});border:none;".format(color.red(), color.green(), color.blue()))
color=QtCore.pyqtProperty(QColor, getColor, setColor)
def enterEvent(self, event):
global anim
anim=QPropertyAnimation(self, "color")
anim.setDuration(200)
anim.setStartValue(QColor(216, 140, 230))
anim.setEndValue(QColor(230, 230, 230))
anim.start()
def leaveEvent(self, event):
self.setStyleSheet("background:none;")
Run Code Online (Sandbox Code Playgroud) 我想获得QPushButtonMainWindow 中的所有列表.实际上,我有一个QRadioButton,当我取消选中它时,我想禁用QPushButton我的所有窗口.
我怎样才能做到这一点 ?
我正在学习使用 Qt 样式表向我的应用程序添加不同的样式。我上网查看了 Qt 文档,其中说您可以使用名为 的东西ID Selector,可以将主题应用于某些对象。这就是我实现此功能的方式:
QPushButton#button
{
color:red;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用。它仅在我尝试不使用时才有效ID Selector:
QPushButton
{
color:red;
}
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?我使用了复制和粘贴,所以我100%确定我没有把名字弄错。
我想在Qt中创建一个垂直按钮(使用C ++,而不是Python),使文本顺时针或逆时针旋转90º。使用标准QPushButton似乎不可能。
我该怎么办?
有没有办法用 QPushButton 制作加载按钮?我想做一些类似于https://coreui.io/docs/components/loading-buttons/但在 Qt 中的事情。这可能吗?
我想从每个列表小部件项目中制作一个加载/进度条,如下所示。
qpushbutton ×10
qt ×7
pyqt ×4
python ×4
c++ ×3
click ×2
pyqt5 ×2
pyside ×2
button ×1
function ×1
hover ×1
mousepress ×1
qmenu ×1
qradiobutton ×1
qstylesheet ×1