简体   繁体   English

如何使用样式表更改 PyQt 按钮的 position?

[英]How to change position of PyQt button using Style Sheet?

I am familiar with CSS so it would be awesome if I could use it.我熟悉 CSS,所以如果我能使用它会很棒。 The problem I have is when positioning using CSS.我遇到的问题是使用 CSS 定位时。

This is the python code:这是 python 代码:

from PyQt5 import QtCore, QtGui, QtWidgets


f=open("style.css", "r")
stylesheet = f.read()
f.close()


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1084, 650)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.saveButton = QtWidgets.QPushButton(self.centralwidget)
        self.saveButton.setObjectName("saveButton")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Wallpaper"))
        self.saveButton.setText(_translate("MainWindow", "Save"))





if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    app.setStyleSheet(stylesheet)
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

And this is css code:这是 css 代码:

QPushButton{
    background-color: #4e4e4e;
    color: #ffffff;
    position: absolute;
    top: 100px;
    left: 100px;
}

TL; TL; DR;博士; No, you cannot change the position of a widget using Qt Style Sheet.不,您不能使用 Qt 样式表更改小部件的 position。


Although the "position" property exists in the Qt Style Sheet, it is not used to move widgets, but rather to establish the position of the internal elements of the widget with respect to the same widget, such as the "down-button" position of the QSpinBox with respect to the QSpinBox,虽然“位置”属性存在于Qt样式表中,但它不是用来移动小部件,而是建立小部件内部元素相对于同一小部件的position,例如“向下按钮”Z4757FE07FD462A8BE0EA6A7D6 QSpinBox 相对于 QSpinBox,

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

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