我有问题改变文本对齐使用pyqt4 desginer我已经使标签水平对齐西方但文本在北向南看起来很糟糕我想改变文本的对齐方式如何可以我这样做......提前谢谢。
这是我的ui.py代码
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.gridLayout = QtGui.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.tabWidget.setTabPosition(QtGui.QTabWidget.West)
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.tab = QtGui.QWidget()
self.tab.setObjectName(_fromUtf8("tab"))
self.tabWidget.addTab(self.tab, _fromUtf8(""))
self.tab_2 = QtGui.QWidget()
self.tab_2.setObjectName(_fromUtf8("tab_2"))
self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
self.tab_3 = QtGui.QWidget()
self.tab_3.setObjectName(_fromUtf8("tab_3"))
self.tabWidget.addTab(self.tab_3, _fromUtf8(""))
self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 31))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2", None))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "Page", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
这是我的主文件,我将添加所有功能,从这里我生成我的窗口
from untitled import *
from PyQt4 import QtGui # Import the PyQt4 module we'll need
import sys # We need sys so that we can pass argv to QApplication
import os
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
我提出的解决方案可能不是确切的解决方案,但我认为它是最接近的解决方案。我的建议是宣传QTabWidget
使用自定义QTabWidget
。
在此之前,我已经改进了此answer中提出的解决方案:
<强> tabwidget.py 强>
from PyQt4 import QtGui, QtCore
class HorizontalTabBar(QtGui.QTabBar):
def paintEvent(self, event):
painter = QtGui.QStylePainter(self)
option = QtGui.QStyleOptionTab()
for index in range(self.count()):
self.initStyleOption(option, index)
painter.drawControl(QtGui.QStyle.CE_TabBarTabShape, option)
painter.drawText(self.tabRect(index),
QtCore.Qt.AlignCenter | QtCore.Qt.TextDontClip,
self.tabText(index))
def tabSizeHint(self, index):
size = QtGui.QTabBar.tabSizeHint(self, index)
if size.width() < size.height():
size.transpose()
return size
class TabWidget(QtGui.QTabWidget):
def __init__(self, parent=None):
QtGui.QTabWidget.__init__(self, parent)
self.setTabBar(HorizontalTabBar())
此文件将存储在.ui文件和.py文件旁边,如以下结构所示:
.
├── main.py # file of class MainWindow(QMainWindow,Ui_MainWindow):
├── tabwidget.py # custom QTabWidget
├── untitled.py
└── untitled.ui # your design
在使用上一个结构后,我们使用Qt Designer打开.ui文件,然后右键单击QTabWidget
并选择提升为... :
将打开一个对话框,其中应放入以下内容:
然后按下add
按钮,再按promote
按钮,最后在pyuic
最后,您将获得以下小部件:
答案 1 :(得分:0)
使用上述方法,然后在其中添加一行代码并显示我的图标
from PyQt4 import QtGui, QtCore
class HorizontalTabBar(QtGui.QTabBar):
def paintEvent(self, event):
painter = QtGui.QStylePainter(self)
option = QtGui.QStyleOptionTab()
for index in range(self.count()):
self.initStyleOption(option, index)
painter.drawControl(QtGui.QStyle.CE_TabBarTabShape, option)
painter.drawText(self.tabRect(index),
QtCore.Qt.AlignCenter | QtCore.Qt.TextDontClip,
self.tabText(index))
if index == 0:
painter.drawImage(QtCore.QRectF(10, 10, 66, 67), QtGui.QImage("ico/HOME.png"))
def tabSizeHint(self, index):
size = QtGui.QTabBar.tabSizeHint(self, index)
size.setHeight=50
size.setWidth=200
if size.width() < size.height():
size.transpose()
return size
class TabWidget(QtGui.QTabWidget):
def __init__(self, parent=None):
QtGui.QTabWidget.__init__(self, parent)
self.setTabBar(HorizontalTabBar())
你可以根据标签的索引添加图标并相应地设置它们的位置,直到现在这是我能给予的最佳解决方案。主持人
答案 2 :(得分:0)
更好的外观和可用性:
在Qt设计器中>
QTabBar::tab {height: 0px;}
这里有个例子:
答案 3 :(得分:0)
我知道已经有一段时间了,但是如果有人需要PyQt5版本的@Sahil Jain的答案,请参考以下内容作为tabwidget.py
from PyQt5 import QtGui, QtCore, QtWidgets
class HorizontalTabBar(QtWidgets.QTabBar):
def paintEvent(self, event):
painter = QtWidgets.QStylePainter(self)
option = QtWidgets.QStyleOptionTab()
for index in range(self.count()):
self.initStyleOption(option, index)
painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, option)
painter.drawText(self.tabRect(index),
QtCore.Qt.AlignCenter | QtCore.Qt.TextDontClip,
self.tabText(index))
def tabSizeHint(self, index):
size = QtWidgets.QTabBar.tabSizeHint(self, index)
if size.width() < size.height():
size.transpose()
return size
class TabWidget(QtWidgets.QTabWidget):
def __init__(self, parent=None):
QtWidgets.QTabWidget.__init__(self, parent)
self.setTabBar(HorizontalTabBar())