If I create my UI in QT Designer (and import UI to the script), how can I hide and show tabs in my script?
class Tool(QMainWindow, uiTool.Ui_Tool):
def __init__(self):
super(Tool, self).__init__()
# SETUP UI
self.setupUi(self)
# self.tabWidget.removeTab() ???
There is no way to hide/show the tabs in a tab-widget, so you will need to remove and replace them instead.
Below is a demo script that shows how to do this. I have not attempted to keep track of the original indexes in this example - it just shows the basic usage of the methods involved:
import sys
from PyQt5 import QtCore, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(354, 268)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.tabWidget = QtWidgets.QTabWidget(Form)
self.tabWidget.setObjectName("tabWidget")
self.tabRed = QtWidgets.QWidget()
self.tabRed.setObjectName("tabRed")
self.tabWidget.addTab(self.tabRed, "")
self.tabBlue = QtWidgets.QWidget()
self.tabBlue.setObjectName("tabBlue")
self.tabWidget.addTab(self.tabBlue, "")
self.tabGreen = QtWidgets.QWidget()
self.tabGreen.setObjectName("tabGreen")
self.tabWidget.addTab(self.tabGreen, "")
self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 2)
self.buttonRemove = QtWidgets.QPushButton(Form)
self.buttonRemove.setObjectName("buttonRemove")
self.gridLayout.addWidget(self.buttonRemove, 1, 0, 1, 1)
self.buttonRestore = QtWidgets.QPushButton(Form)
self.buttonRestore.setObjectName("buttonRestore")
self.gridLayout.addWidget(self.buttonRestore, 1, 1, 1, 1)
self.retranslateUi(Form)
self.tabWidget.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabRed), _translate("Form", "Red"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabBlue), _translate("Form", "Blue"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tabGreen), _translate("Form", "Green"))
self.buttonRemove.setText(_translate("Form", "Remove"))
self.buttonRestore.setText(_translate("Form", "Restore"))
class Window(QtWidgets.QWidget, Ui_Form):
def __init__(self):
super(Window, self).__init__()
self.setupUi(self)
self.buttonRemove.clicked.connect(self.handleButtonRemove)
self.buttonRestore.clicked.connect(self.handleButtonRestore)
self.tab_pages = []
for index in range(self.tabWidget.count()):
self.tab_pages.append((
self.tabWidget.widget(index),
self.tabWidget.tabText(index),
))
def handleButtonRemove(self):
index = self.tabWidget.currentIndex()
if index >= 0:
self.tabWidget.removeTab(index)
def handleButtonRestore(self):
for page, title in self.tab_pages:
if self.tabWidget.indexOf(page) < 0:
self.tabWidget.addTab(page, title)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 300, 200)
window.show()
sys.exit(app.exec_())
Related
I need a signal or event from the QtableView to capture the row number user selects
Here I tried an event filter to get the Mouse Button Press event, but nothing got filtered out, yet the same filter works for Context Menu event, Is there any related method to capture signal from table view? (like cellClicked.connect in QTableWidget)
from PyQt6 import QtCore, QtGui, Qt6
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QDialog, QApplication, QWidget, QMainWindow, QHeaderView, QTableView
from PyQt6 import uic
import sys
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data):
super(TableModel, self).__init__()
self.data = data
def data(self, index, role):
if role == Qt.ItemDataRole.DisplayRole:
return self.data[index.row()][index.column()]
def rowCount(self, index):
return len(self.data)
def columnCount(self, index):
return len(self.data[0])
class UI(QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi('model.ui', self)
self.tableView.installEventFilter(self)
data = [[4, 9, 2], [1, 0, 0], [3, 5, 0], [3, 3, 2], [7, 8, 9]]
self.model = TableModel(data)
self.tableView.setModel(self.model)
def selected_row(self):
indexes = self.tableView.selectedIndexes()
if indexes:
index = indexes[0]
row = index.row()
print(row)
def eventFilter(self, source, event):
if event.type() == event.Type.MouseButtonPress and source == self.tableView:
self.selected_row()
return super().eventFilter(source, event)
def except_hook(cls, exception, traceback):
sys.__excepthook__(cls, exception, traceback)
if __name__=="__main__":
app = QApplication([])
window = UI()
window.show()
sys.excepthook = except_hook
sys.exit(app.exec())
I have an application with multiple buttons
When pressed a button, the text on the pressed button has to be changed.
This can be done with a separate function for each button, but I want to use 1 function to keep the code short.
Code so far:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(200, 200, 700, 500)
self.setWindowTitle("Tic Tac Toe")
self.initUI()
def initUI(self):
font = QtGui.QFont()
font.setPointSize(50)
font.setBold(True)
font.setWeight(75)
self.btn1 = QtWidgets.QPushButton(self)
self.btn1.setGeometry(QtCore.QRect(90, 50, 93, 91))
self.btn1.setFont(font)
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(lambda:self.function("btn1"))
self.btn2 = QtWidgets.QPushButton(self)
self.btn2.setGeometry(QtCore.QRect(210, 50, 93, 91))
self.btn2.setFont(font)
self.btn2.setText("")
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(lambda:self.function("btn2"))
font2 = QtGui.QFont()
font2.setPointSize(16)
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(470, 50, 201, 51))
self.label.setFont(font2)
self.label.setObjectName("label")
def function(self, btnnmbr):
self.label.setText("knop " + btnnmbr)
#self.btn1.setText("X")
# Mainloop
def window():
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
window()
Any idea how to change the text of the clicked instance of a button?
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(200, 200, 700, 500)
self.setWindowTitle("Tic Tac Toe")
self.initUI()
def initUI(self):
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.btn1 = QtWidgets.QPushButton(self)
self.btn1.setGeometry(QtCore.QRect(90, 50, 93, 91))
self.btn1.setFont(font)
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(lambda ch, btn=self.btn1: self.function(btn)) # +
self.btn2 = QtWidgets.QPushButton(self)
self.btn2.setGeometry(QtCore.QRect(210, 50, 93, 91))
self.btn2.setFont(font)
self.btn2.setText("")
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(lambda ch, btn=self.btn2: self.function(btn)) # +
font2 = QtGui.QFont()
font2.setPointSize(16)
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(470, 50, 201, 51))
self.label.setFont(font2)
self.label.setObjectName("label")
def function(self, btn): # + btn
self.label.setText(f"knop {btn.objectName()}") # +
btn.setText(btn.objectName()) # +++
#self.btn1.setText("X")
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
It's been a while I'm searching all over the internet for that, but still couldn't get the answer... basically I have a QVBoxLayout which contains several frames - and each frame contains a button which function is to delete its parent frame. The main code was created using QT Designer and pyuic4. I wrote the two additional functions, one works perfectly ("createFrame") but I'm struggling with the "deleteFrame" one. the label inside the frame shows the index of that frame in the vert_layout "array". the problem I noted on my code is that, using it that way, I can only delete the last frame added. So, can someone help me with this issue?
please see my code below:
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
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)
#Custom button that sends out its own instance as the signal
class MyPushButton(QtGui.QPushButton):
mySignal = QtCore.pyqtSignal(QtGui.QWidget)
def mousePressEvent(self, *args, **kwargs):
self.mySignal.emit(self)
class Functions:
def createFrame(self,mainWindow):
#just local frame no need to store it in the class
frame = QtGui.QFrame()
frame.setGeometry(QtCore.QRect(10, 10, 241, 61))
frame.setFrameShape(QtGui.QFrame.StyledPanel)
frame.setFrameShadow(QtGui.QFrame.Raised)
frame.setObjectName(_fromUtf8("frame"))
pushButton_2 = MyPushButton(frame)
pushButton_2.setGeometry(QtCore.QRect(94, 10, 141, 41))
pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
label = QtGui.QLabel(frame)
label.setGeometry(QtCore.QRect(20, 10, 71, 41))
label.setFrameShape(QtGui.QFrame.NoFrame)
label.setAlignment(QtCore.Qt.AlignCenter)
#the vert_layout belongs to class Ui_MainWindow
mainWindow.vert_layout.addWidget(frame)
label.setObjectName(_fromUtf8("label"))
label.setText(str(mainWindow.vert_layout.indexOf(frame)))
pushButton_2.setText(_translate("MainWindow", "delete this frame and all\n its childs", None))
pushButton_2.mySignal.connect(self.deleteFrame)
#my initial idea was to include the mainWindow (instance of
#Ui_MainWindow class in order to use vert_layout - but apparently
#I need to modify the signal as well, right?
def deleteFrame(self,ref,mainWindow):
#finding the index of the FRAME (mybutton's parent)
#that is to be deleted
frame = mainWindow.vert_layout.itemAt(self.vert_layout.indexOf(ref.parent()))
widget = frame.widget()
if widget is not None:
widget.deleteLater()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(400, 284)
self.vert_layout = QtGui.QVBoxLayout()
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.scrollArea = QtGui.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(10, 10, 281, 261))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 277, 257))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.scrollAreaWidgetContents.setLayout(self.vert_layout)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(310, 20, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
f = Functions()
self.pushButton.clicked.connect(lambda: f.createFrame())
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "create", None))
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__== '__main__':
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
All you are currently doing is deleting the self.frame widget each time.
So only the last created one gets deleted. And not the one you actually click on.
Modified your code for what you need. Hope it helps you understand.
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
import sys
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)
#Custom button that sends out its own instance as the signal
class MyPushButton(QtGui.QPushButton):
mySignal = QtCore.pyqtSignal(QtGui.QWidget)
def mousePressEvent(self, *args, **kwargs):
self.mySignal.emit(self)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(400, 284)
self.vert_layout = QtGui.QVBoxLayout()
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.scrollArea = QtGui.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(10, 10, 281, 261))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 277, 257))
self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
self.scrollAreaWidgetContents.setLayout(self.vert_layout)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(310, 20, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(lambda: self.createFrame())
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "create", None))
def createFrame(self):
#just local frame no need to store it in the class
frame = QtGui.QFrame()
frame.setGeometry(QtCore.QRect(10, 10, 241, 61))
frame.setFrameShape(QtGui.QFrame.StyledPanel)
frame.setFrameShadow(QtGui.QFrame.Raised)
frame.setObjectName(_fromUtf8("frame"))
pushButton_2 = MyPushButton(frame)
pushButton_2.setGeometry(QtCore.QRect(94, 10, 141, 41))
pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
label = QtGui.QLabel(frame)
label.setGeometry(QtCore.QRect(20, 10, 71, 41))
label.setFrameShape(QtGui.QFrame.NoFrame)
label.setAlignment(QtCore.Qt.AlignCenter)
self.vert_layout.addWidget(frame)
label.setObjectName(_fromUtf8("label"))
label.setText(str(self.vert_layout.indexOf(frame)))
pushButton_2.setText(_translate("MainWindow", "delete this frame and all\n its childs", None))
pushButton_2.mySignal.connect(self.deleteFrame)
def deleteFrame(self,ref):
#finding the index of the FRAME (mybutton's parent)
#that is to be deleted
frame = self.vert_layout.itemAt(self.vert_layout.indexOf(ref.parent()))
widget = frame.widget()
if widget is not None:
widget.deleteLater()
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
if __name__== '__main__':
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
I cannot find a way to close the tab that request to be closed. I only found how to close current widget and current tab. Any ideas? I can't find a clue on internet.
self.tabs.tabCloseRequested.connect(self.removeTab)
def removeTab(self):
tab = self.tabs.currentWidget()
tab.deleteLater()
The QTabWidget class has a removeTab method, which will remove a tab, but without deleting its content widget. So you will also need to delete the content widget explicitly:
self.tabs.tabCloseRequested.connect(self.removeTab)
...
def removeTab(self, index):
widget = self.tabs.widget(index)
if widget is not None:
widget.deleteLater()
self.tabs.removeTab(index)
I can post you the code, but it's a bit confused.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import codecs
import os
class Central_widget(QWidget):
def __init__(self, setted_lang):
super(Central_widget, self).__init__()
#tab list
self.tab_list = {}
self.cont_list = {}
self.label_list = {}
self.label_num = 0
self.tab_num = 0
#main page contenent
lesson = codecs.open('lessons\lekcja.html', 'r', encoding='utf-8' )
self.read = lesson.read()
read2 = self.read.encode('utf8')
print read2
#General widget
self.general = QWidget(self)
self.general.resize(900, 700)
#leftWidget
self.leftWidget=QWidget(self.general)
self.leftWidget.resize(160 ,657)
#tab master
self.tabmaster(self.general)
def tabmaster(self, general):
#left widget
self.scrollarea()
#right widget
self.tabs = QTabWidget(self.general)
self.tabs.setTabsClosable(True)
self.tabs.setTabEnabled(self.tab_num, True)
self.tabs.resize(710, 640)
self.tabs.move(170, 9)
#actual open tabs
self.actual_numtabs = self.tabs.indexOf(self.tabs.currentWidget())
#homepage tab
self.new_tab('hey')
def new_tab(self, text):
#tab_name - tab_value dictionary
if self.tab_num == 0:
name_new_tab = 'Homepage'
else: name_new_tab = 'Karta '+str(self.tab_num)
value_new_tab = QWidget()
self.tab_list[name_new_tab] = value_new_tab
#tab_content and cont_value
tab_content = 'cont '+str(self.tab_num)
cont_value = QTextBrowser(self.tab_list[name_new_tab])
self.cont_list[tab_content] = cont_value
#set size, text, add widget to tab
self.tabs.addTab(self.tab_list[name_new_tab], str(name_new_tab))
self.cont_list[tab_content].setFixedSize(704, 614)
if self.tab_num == 0:
self.cont_list[tab_content].setText(self.read)
self.tabs.setCurrentWidget(self.tab_list[name_new_tab])
else: self.cont_list[tab_content].setText(text)
#close tab
self.tabs.tabCloseRequested.connect(self.removeTab)
self.tab_num += 1
def removeTab(self, index):
#remove tab from widget
widget = self.tabs.widget(index)
if widget is not None:
widget.deleteLater()
self.tabs.removeTab(index)
def scrollarea(self):
#scroll area
layoutleft = QVBoxLayout(self.leftWidget)
s=QScrollArea()
layoutleft.addWidget(s)
widleft = QWidget(self)
vbox=QVBoxLayout(widleft)
nl=QHBoxLayout()
nl.addWidget(QLabel(u"Lista lekcji dostępnych:", self))
vbox.addLayout(nl)
nl=QHBoxLayout()
vbox.addLayout(nl)
vbox.setSpacing(2)
vbox.insertSpacing(1, 20)
#list of lessons
for filename in os.listdir('lessons'):
name = os.path.splitext(filename)
label_var = name[0]
value = QPushButton(label_var)
self.label_list[label_var] = value
self.label_list[label_var].clicked.connect(self.openlesson)
self.label_list[label_var].setObjectName(label_var)
_l=QHBoxLayout()
_l.addWidget(self.label_list[label_var])
vbox.addLayout(_l)
s.setWidget(widleft)
def openlesson(self):
a = self.sender()
print a.objectName()
lesson = codecs.open('lessons\\'+str(a.objectName())+'.html', 'r', encoding='utf-8' )
read = lesson.read()
self.new_tab(read)
I am working with a QMainWindow and adding a QTableView widget. The table is to be filled with data from a csv file. The csv file first row has the headers, but I cannot find how to write that row into the headers. Even inputting a test header list does not work.
Also I want to reverse sort on the "time" column.
Here is code restricted to mostly the table:
import sys
import csv
from PyQt4 import QtGui
from PyQt4.QtCore import *
from array import *
class UserWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(UserWindow, self).__init__()
self.specModel = QtGui.QStandardItemModel(self)
self.specList = self.createSpecTable()
self.initUI()
def specData(self):
with open('testFile.csv', 'rb') as csvInput:
for row in csv.reader(csvInput):
if row > 0:
items = [QtGui.QStandardItem(field) for field in row]
self.specModel.appendRow(items)
def createSpecTable(self):
self.specTable = QtGui.QTableView()
# This is a test header - different from what is needed
specHdr = ['Test', 'Date', 'Time', 'Type']
self.specData()
specM = specTableModel(self.specModel, specHdr, self)
self.specTable.setModel(specM)
self.specTable.setShowGrid(False)
vHead = self.specTable.verticalHeader()
vHead.setVisible(False)
hHead = self.specTable.horizontalHeader()
hHead.setStretchLastSection(True)
self.specTable.sortByColumn(3, Qt.DescendingOrder)
return self.specTable
def initUI(self):
self.ctr_frame = QtGui.QWidget()
self.scnBtn = QtGui.QPushButton("Sample")
self.refBtn = QtGui.QPushButton("Reference")
self.stpBtn = QtGui.QPushButton("Blah")
# List Window
self.specList.setModel(self.specModel)
# Layout of Widgets
pGrid = QtGui.QGridLayout()
pGrid.setSpacing(5)
pGrid.addWidget(self.scnBtn, 3, 0, 1, 2)
pGrid.addWidget(self.refBtn, 3, 2, 1, 2)
pGrid.addWidget(self.stpBtn, 3, 4, 1, 2)
pGrid.addWidget(self.specList, 10, 0, 20, 6)
self.ctr_frame.setLayout(pGrid)
self.setCentralWidget(self.ctr_frame)
self.statusBar()
self.setGeometry(300, 300, 400, 300)
self.setWindowTitle('Test')
class specTableModel(QAbstractTableModel):
def __init__(self, datain, headerdata, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.arraydata = datain
self.headerdata = headerdata
def rowCount(self, parent):
return len(self.arraydata)
def columnCount(self, parent):
return len(self.arraydata[0])
def data(self, index, role):
if not index.isValid():
return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
return QVariant(self.arraydata[index.row()][index.column()])
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.headerdata[col]
return None
def main():
app = QtGui.QApplication(sys.argv)
app.setStyle(QtGui.QStyleFactory.create("plastique"))
ex = UserWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
and here is a really short csv file:
Run,Date,Time,Comment
data1,03/03/2014,00:04,Reference
data2,03/03/2014,02:00,Reference
data5,03/03/2014,02:08,Sample
data6,03/03/2014,13:57,Sample
Also the rowCount & columnCount definitions do not work.
Worked out answers to what I posted: Wrote a 'getHeader' function simply to read the first line of the csv file and returned the list. Added the following to the createSpecTable function:
specHdr = self.getHeader()
self.specModel.setHorizontalHeaderLabels(specHdr)
self.specModel.sort(2, Qt.DescendingOrder)
The last statement solved the reverse sort problem. The header line from the csv file was removed from the table by adding a last line to the specData function:
self.specModelremoveRow(0).
Finally the rowCount and columnCount were corrected with:
def rowCount(self, parent):
return self.arraydata.rowCount()
def columnCount(self, parent):
return self.arraydata.columnCount()