Code Guy
Code Guy

Reputation: 61

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. The problem I have is when positioning using CSS.

This is the python code:

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:

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

Upvotes: 1

Views: 1216

Answers (1)

eyllanesc
eyllanesc

Reputation: 244361

TL; DR; No, you cannot change the position of a widget using Qt Style Sheet.


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,

Upvotes: 4

Related Questions