I have an app with hundreds of custom buttons, each one needs multiple signal connections. The connect calls seem to be pretty slow, so I'm trying to connect/disconnect each button's signals via the main window's eventFilter using the enter and leave events.
However, sometimes those events seems to be called multiple times, causing RuntimeErrors (when trying to disconnect an event that is already gone).
Here is a snippet of code that shows a similar (and hopefully related) problem using default PushButtons.
To see the runtime error here, run the code, push one of the buttons, then close the window. That's when I see this:
RuntimeError: Fail to disconnect signal clicked().
Here is the code. Does anybody know if this is a PySide bug?
from PySide.QtGui import *
from PySide.QtCore import *
import sys
class TestWindow( QWidget ):
def __init__( self, parent=None ):
super( TestWindow, self ).__init__( parent )
self.setLayout( QGridLayout() )
def addWidget( self, w ):
self.layout().addWidget( w )
def testCB( self ):
print 'button connected'
def eventFilter( self, obj, event ):
'''Connect signals on mouse over'''
if event.type() == QEvent.Enter:
print 'enter',
obj.clicked.connect( self.testCB )
elif event.type() == QEvent.Leave:
print 'leave'
obj.clicked.disconnect( self.testCB )
return False
app = QApplication( sys.argv )
w = TestWindow()
for i in xrange(10):
btn = QPushButton( 'test %s' % i )
w.addWidget( btn )
btn.installEventFilter(w)
w.show()
sys.exit( app.exec_() )
In few cases, when I tested mouse events, showed better performance while events are attached to item class... so don't subclass. Rather :
class Button(QPushButton):
def __init__(self, label):
super(Button, self).__init__()
self.setText(label)
app = QApplication( sys.argv )
w = TestWindow()
for i in xrange(10):
btn = Button( 'test %s' % i )
w.addWidget( btn )
...then define mouse event for class.
Related
I have a scenario where I am outsourcing my execution part to QThread and during execution i have a need to launch QDialog to take some user inputs within QThread.
For scenario where QDialog calls are not in picture is working fine but for snippet where QDialog code runs, i get
QPixmap: It is not safe to use pixmaps outside the GUI thread
QObject::startTimer: timers cannot be started from another thread
QApplication: Object event filter cannot be in a different thread.
and execution stops abruptly.
My QDialog code :
class ResultDialog(QDialog):
def __init__(self, parent = None):
super(ResultDialog, self).__init__(parent)
self.resultIndex = -1
grid_layout = QGridLayout(self)
failreplace_layout = QHBoxLayout(self)
faildontreplace_layout = QHBoxLayout(self)
self.info_lbl = QLabel(self)
self.info_lbl.setText("Image comparision failed for screen resolution")
self.failreplace_radio = QRadioButton(self)
self.failreplace_radio.setText("Fail and Replace Reference Image")
self.faildontreplace_radio = QRadioButton(self)
self.faildontreplace_radio.setText("Fail and Do not replace Reference Image")
self.tester_comment = QPlainTextEdit(self)
self.tester_comment.clear()
self.tester_comment.setPlainText('Tester comment is desired')
self.tester_comment.setDisabled(True)
self.buttonsend = QPushButton(self)
self.buttonsend.setText("Ok")
self.buttonsend.setCheckable(True)
grid_layout.addWidget(self.info_lbl,0,0)
grid_layout.addWidget(self.failreplace_radio,1,0)
grid_layout.addWidget(self.faildontreplace_radio,2,0)
grid_layout.addWidget(self.tester_comment,3,0)
self.loop = QtCore.QEventLoop()
# OK and Cancel buttons
grid_layout.addWidget(self.buttonsend, 4,0)
self.buttonsend.clicked.connect(self.submitclose)
self.failreplace_radio.clicked.connect(self.onfailreplace_radio)
self.faildontreplace_radio.clicked.connect(self.onfaildontreplace_radio)
def submitclose(self):
self.loop.quit()
self.accept()
def onfailreplace_radio(self):
print "onfailreplace_radio "
self.tester_comment.setDisabled(False)
self.buttonsend.setDisabled(False)
self.tester_comment.clear()
self.resultIndex = 0
def onfaildontreplace_radio(self):
print "onfaildontreplace_radio "
self.tester_comment.setDisabled(False)
self.buttonsend.setDisabled(False)
self.tester_comment.clear()
self.resultIndex = 1
# static method to create the dialog and return
#staticmethod
def returnSelection(parent):
dialog = ResultDialog(parent)
result = dialog.show()
dialog.loop.exec_();
print "dialog.buttonsend.isChecked() ", dialog.buttonsend.isChecked()
if dialog.buttonsend.isChecked() == True:
if not str(dialog.tester_comment.toPlainText()):
QMessageBox.critical(dialog, 'Tester Comment', 'Tester comment is desired')
return (dialog.resultIndex, 'NA')
else:
return (dialog.resultIndex, str(dialog.tester_comment.toPlainText()))
if __name__ == "__main__":
app = QApplication([])
ok = ResultDialog.returnSelection()
print("{}".format(ok))
app.exec_()
And i am calling it from Qthread.run() like :
index, testercomment = ResultDialog.returnSelection(None)
if index == 0 or index == 1:
self.resumeExec()
else:
self.sync.lock();
self.pauseCond.wait(self.sync)
self.sync.unlock();
I want my application to terminate all drag and drops in a dragLeaveEvent, without the user releasing the mouse button.
The problem is that the loop suspends all events that could cancel a QDrag while it is happening, even though the documentation states:
"On Linux and Mac OS X, the drag and drop operation can take some
time, but this function does not block the event loop. Other events
are still delivered to the application while the operation is
performed. On Windows, the Qt event loop is blocked during the
operation. However, QDrag.exec() on Windows causes processEvents() to
be called frequently to keep the GUI responsive. If any loops or
operations are called while a drag operation is active, it will block
the drag operation."
Because of this, I cannot call events which would end the drag.
So far, I've tried what is suggested here, as seen in the code. I'm using PyQt5, but if a solution works in Qt it should work in PyQt.
Edit: I'm a little scared to delete the drag, as the scene does not own it. I suppose I could set it up to own it though, but as was posted here it should not work.
Edit2: Added code with my non-working attempts to fix it. I'd really like to solve this issue without having to make my own drag-drop framework. Also trimmed post.
import sys
from PyQt5.QtWidgets import (QMainWindow, QApplication,
QGraphicsView, QGraphicsScene, QGraphicsWidget, QGraphicsRectItem)
from PyQt5.QtCore import (QMimeData, Qt, QByteArray, QCoreApplication,
QEvent, QPoint)
from PyQt5.QtGui import QBrush, QColor, QDrag, QPen, QMouseEvent
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.scene = CustomScene()
self.view = QGraphicsView(self.scene, self)
self.setGeometry(100, 100, 600, 600)
self.view.setGeometry(0, 0, 500, 500)
self.show()
class CustomScene(QGraphicsScene):
def __init__(self):
super().__init__()
self.customWidgets = []
for i in range(5):
newItem = CustomDragWidget()
self.addItem(newItem)
self.customWidgets.append(newItem)
newItem.setGeometry(i * 50, i * 50, 50, 50)
def dragLeaveEvent(self, event):
# Work your magic here. I've tried the following:
# 1)
self.customWidgets[0].dropEvent(event)
# 2)
self.dropEvent(event)
# 3)
eve = QMouseEvent(QEvent.MouseButtonRelease, QPoint(0, 0), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
QCoreApplication.sendEvent(self.views()[0], eve)
QCoreApplication.processEvents()
# 4)
eve = QMouseEvent(QEvent.MouseButtonRelease, QPoint(0, 0), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
QCoreApplication.sendEvent(self.customWidgets[0], eve)
QCoreApplication.processEvents()
def dropEvent(self, QGraphicsSceneDragDropEvent):
# a dummy dropevent that tries to stop the drop, but doesnt work
QGraphicsSceneDragDropEvent.accept()
class CustomDragWidget(QGraphicsWidget):
def __init__(self,):
super().__init__()
self.squareItem = QGraphicsRectItem()
self.squareItem.setBrush(QBrush(QColor(Qt.blue)))
self.squareItem.setPen(QPen(QColor(Qt.black), 2))
self.squareItem.setRect(0, 0, 50, 50)
self.squareItem.setParentItem(self)
self.setAcceptDrops(True)
def mousePressEvent(self, event):
mime = QMimeData()
itemData = QByteArray()
mime.setData('application/x-dnditemdata', itemData)
drag = QDrag(self)
drag.setMimeData(mime)
drag.exec(Qt.MoveAction)
def dropEvent(self, event):
event.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
This is a little bit hackish, but it seems to work (on Linux, anyway):
def dragLeaveEvent(self, event):
QCoreApplication.postEvent(self,
QKeyEvent(QEvent.KeyPress, Qt.Key_Escape, Qt.NoModifier))
I am super new to PyQT ... I am using QT Designer for my forms. I am confused about where to put my event handling for the second form in my project.
I have the following .py files :
main.py
Ui_make_render_folders.py (generated from the Qt designer ui file)
Ui_prefs.py (generated from the Qt designer ui file)
Mainwindow.py (this is where all the even handling for the main form
lives)
icons_rc.py (resource file)
make_render_folders.py (where all my custom functions live)
Here is a picture of the script running :
click to see script running << CLICK HERE
My question is ... where does the even handling for my prefs window go ???? I cant figure out where to put my [b]on_btn_released() for the buttons on y 2nd form.[/b] Also I am not sure I am instantiating that dialog properly. Currently I am adding this code to the top of my MainWindow.py.
Finally what does #pyqSignature("") do ? And do I need it ?
"""
This is where all teh MainWindow logic sits
"""
import os
import os.path
from PyQt4.QtGui import QMainWindow, QFileDialog, QTreeWidgetItem, QDialog
from PyQt4.QtCore import pyqtSignature, QString
from make_folders import make_render_folders
from Ui_make_render_folders import Ui_MainWindow
from Ui_prefs import Ui_prefs
class Prefs(QDialog, Ui_prefs):
def __init__(self, parent = None):
QDialog.__init__(self, parent)
self.setupUi(self)
#pyqtSignature("")
def on_btn_get_ref_dir_released(self):
print '2nd form'
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent = None):
"""
Constructor
"""
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.prefs = QDialog()
ui = Ui_prefs()
ui.setupUi(self.prefs)
#pyqtSignature("")
def on_btn_process_released(self):
print 'process'
no_of_shots = self.spn_no_of_shots.value()
#spot_path = self.le_proj_path.text()+'\\'+self.lst_spots.selectedItems()[0].text()
spot_path_string = 'self.le_proj_path.text())+os.sep,'
spot_path_string += str(self.lst_spots.selectedItems()[0].text())
os.path.join(spot_path_string)
save_position = self.lst_spots.selectedItems()[0]
if no_of_shots > 0:
if self.cmb_mode.currentText() == 'Create Shots':
print ('creating shots')
for x in range(1, no_of_shots+1):
make_render_folders.create_shot_folder(spot_path, x)
else:
print ('inserting shots')
if self.lst_shots.count() > 0:
t =self.lst_shots.selectedItems()[0].text()
cur_shot = t[2:]
cur_shot_val = int(cur_shot)
cur_index = self.lst_shots.currentRow()
print('sel_value :'+cur_shot)
print('sel_index :'+str(cur_index))
next_shot_index = int(cur_index)+1
print('nextshot_index ='+str(next_shot_index))
next_shot_text = self.lst_shots.item(next_shot_index).text()
next_shot_val = int(next_shot_text[2:])
print('nextshot value ='+str(next_shot_val))
insert_space = next_shot_val - cur_shot_val
print(str(insert_space))
if no_of_shots > (insert_space-1) :
print "not enough space - please reduce shots to insert"
else:
print('insert the shots')
for x in range(cur_shot_val,(cur_shot_val+no_of_shots) ):
print (str(x))
make_render_folders.insert_shot_folder(spot_path, x+1)
make_render_folders.populate_shot_list(self)
self.lst_shots.setCurrentRow(0)
#pyqtSignature("")
def on_btn_quit_released(self):
print 'quit'
self.close()
#pyqtSignature("")
def on_cmb_mode_currentIndexChanged(self, ret_text):
print ret_text
#pyqtSignature("")
def on_le_proj_path_editingFinished(self):
print "editingFinished le_proj_path"
def on_le_new_spot_name_textChanged(self):
if len(self.le_new_spot_name.text()) > 0 :
self.btn_add_spot.setEnabled(True)
else :
self.btn_add_spot.setEnabled(False)
#pyqtSignature("")
def on_btn_add_spot_released(self):
v_NewSpotFolder = self.le_new_spot_name.text()
v_rPath = self.le_proj_path.text()
x = make_render_folders.create_spot_folder(v_NewSpotFolder,v_rPath)
if x :
self.le_new_spot_name.clear()
make_render_folders.populate_spots_list(self)
#pyqtSignature("")
def on_actionLoad_Project_triggered(self):
print "actionLoad_Project"
self.lst_shots.clear()
self.lst_spots.clear()
proj_dir = str(QFileDialog.getExistingDirectory(self, 'Select Project'))
print proj_dir
if os.path.exists(proj_dir):
print 'the file is there'
elif os.access(os.path.dirname(proj_dir), os.W_OK):
print 'the file does not exists but write privileges are given'
else:
print 'can not write there'
p = os.path.join(proj_dir+os.sep,'renders')
self.le_proj_path.setText(p)
make_render_folders.populate_spots_list(self)
#pyqtSignature("")
def on_actionConfig_triggered(self):
print "config"
self.prefs.show()
#pyqtSignature("")
def on_actionQuit_triggered(self):
print "ActionQuit"
#pyqtSignature("")
def on_btn_get_folders_released(self):
make_render_folders.populate_spots_list(self)
def on_lst_spots_itemClicked (self):
print "got you"
self.lst_shots.clear()
make_render_folders.populate_shot_list(self)
Thanks !!!
Event handling for the Prefs window can go anywhere. Personally I always prefer to give each window its own file.
So the py files would look something like:
Mainwindow.py (Main form events etc)
Ui_make_render_folders.py (Qt Designer generated UI)
prefs.py (Prefs form events etc)
ui_prefs.py (Qt Designer generated UI)
your other files...
Initialisation: (MainWindow.py)
from PyQt4.QtGui import QMainWindow
from Ui_make_render_folders import Ui_MainWindow
#prefs here refers to the file: prefs.py. PrefWindow is the class within this file.
from prefs import PrefWindow
class MainWindow(QMainWindow):
def __init__(self, parent=None):
#Better way to initialise
super(MainWindow, self).__init__(parent)
self.main = Ui_MainWindow()
self.main.setupUi(self)
#initialise prefs to variable
self.prefs = PrefWindow(self)
# Rest of init...
Note: initialising of second form will be similar to above.
You should place all events for prefs form within its class.
From the function that calls Prefs:
#pyqtSlot()
def on_actionConfig_triggered(self):
print "config"
self.prefs.exec_() # Will block main window while its open.
# Use self.prefs.show() if you want user to be able to interact with main window
Finally: pyqtSignature
On http://pyqt.sourceforge.net/Docs/PyQt4/old_style_signals_slots.html it states that:
'The QtCore.pyqtSignature() serves the same purpose as the pyqtSlot() decorator but has a less Pythonic API.'
Better to use pyqtSlot()
I'm trying to handle the createWindow method in PyQt webkit. When you do this, PyQt gives the method a QWebPage.WebWindowType (or QWebPage::WebWindowType in c++). All I need is a URL to create a new window, so I'm not sure what to do with this class. The docs don't explain it very well either: http://doc.qt.digia.com/qt/qwebpage.html#WebWindowType-enum
I'm trying to intergrate this with my "Tab" class:
class Tab(QtGui.QWidget):
"""
Contains the variables and methods needed to open the Qt
based user interface and the event bus to allow interaction.
"""
def __init__(self, url, window):
"""
Set the up the following:
1) Initialise the Qt user interface.
2) Set margins.
3) Initialise the cookie jar.
4) Set the default URL.
5) Set the history buttons to disabled (there is not history yet).
5) Hide the tools menu.
6) Create the GUI event bus.
7) Set keyboard bindings.
"""
self.window = window
QtGui.QWidget.__init__(self)
self.ui = Ui_Browser()
self.ui.setupUi(self)
l = self.layout()
l.setMargin(0)
self.ui.horizontalLayout.setMargin(0)
try:
self.ui.webView.page().networkAccessManager().setCookieJar(self.window.cookies)
except:
pass
self.ui.webView.setUrl(url)
self.ui.url.setText(url.toString())
self.ui.back.setEnabled(False)
self.ui.forward.setEnabled(False)
self.ui.toolsBox.hide()
self.ui.tools.setChecked(False)
QtCore.QObject.connect(self.ui.back, QtCore.SIGNAL("clicked()"), self.back)
QtCore.QObject.connect(self.ui.forward, QtCore.SIGNAL("clicked()"), self.forward)
QtCore.QObject.connect(self.ui.refresh, QtCore.SIGNAL("clicked()"), self.refresh)
QtCore.QObject.connect(self.ui.url, QtCore.SIGNAL("returnPressed()"), self.url)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("linkClicked (const QUrl&)"), self.navigate)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("urlChanged (const QUrl&)"), self.navigate)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("titleChanged(const QString&)"), self.title)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("loadStarted()"), self.showProgressBar)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("loadProgress(int)"), self.setProgressBar)
QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("loadFinished(bool)"), self.hideProgressBar)
QtCore.QObject.connect(self.ui.toolPrint, QtCore.SIGNAL("clicked()"), self.printPage)
self.ui.webView.createWindow = self.createWindow
QtGui.QShortcut(QtGui.QKeySequence("Backspace"), self, self.back)
QtGui.QShortcut(QtGui.QKeySequence("F5"), self, self.refresh)
QtGui.QShortcut(QtGui.QKeySequence("Ctrl+p"), self, self.printPage)
def url(self):
"""
Go to URL entered.
"""
self.setHistory()
url = self.ui.url.text()
if not str(url).startswith("http://"):
url = "http://" + url
self.ui.webView.setUrl(QtCore.QUrl(url))
def navigate(self, url):
"""
Navigate to page requested.
"""
self.setHistory()
self.ui.url.setText(url.toString())
def refresh(self):
"""
Refresh the page.
"""
self.ui.webView.setUrl(QtCore.QUrl(self.ui.webView.url()))
def back(self):
"""
Enable the back button.
"""
page = self.ui.webView.page()
history = page.history()
history.back()
if history.canGoBack():
self.ui.back.setEnabled(True)
else:
self.ui.back.setEnabled(False)
def forward(self):
"""
Enable the forward button.
"""
page = self.ui.webView.page()
history = page.history()
history.forward()
if history.canGoForward():
self.ui.forward.setEnabled(True)
else:
self.ui.forward.setEnabled(False)
def title(self, title):
"""
Change the title of the window.
"""
self.window.tabs.setTabText(self.window.tabs.indexOf(self), title) or (self.window.setWindowTitle(title) if self.isThisFocused() else "New Tab")
def createWindow(self, WebWindowType):
"""
Overide the default behaviour.
"""
tab = Tab(QtCore.QUrl("about:blank"), self.window)
print(mode)
if mode == QtWebKit.QWebPage.WebModalDialog:
tab.ui.webView.setWindowModality(QtCore.Qt.ApplicationModal)
self.window.tabs.setCurrentIndex(self.window.tabs.addTab(tab, ""))
def isThisFocused(self):
"""
Return whether this tab is focused or not.
"""
if self.window.tabs.currentWidget() == self:
return True
return False
def setHistory(self):
"""
Check history and update buttons.
"""
page = self.ui.webView.page()
history = page.history()
if history.canGoBack():
self.ui.back.setEnabled(True)
else:
self.ui.back.setEnabled(False)
if history.canGoForward():
self.ui.forward.setEnabled(True)
else:
self.ui.forward.setEnabled(False)
def showProgressBar(self):
"""
We're loading a page, show the load progress bar.
"""
self.ui.progress.show()
def setProgressBar(self, percent):
"""
Set the percentage of the progress bar.
"""
self.ui.progress.setValue(percent)
def hideProgressBar(self, bool):
"""
We've finished loading the page, there is no need for the progress bar.
"""
self.ui.progress.hide()
def printPage(self):
"""
Use Qt's goodness to print the page.
"""
previewer = QtGui.QPrintPreviewDialog(paintRequested=self.ui.webView.print_)
previewer.exec_()
The createWindow function is used for handling requests to open a new window (e.g. a javascript window.open() call).
By default, createWindow does nothing, so it must be reimplemented it in a subclass.
Also, to work with javascript, it is necessary that the web-settings specify that javascript programs are allowed to open new windows.
Here's a demo script that illustrates how to use createWindow:
from PyQt4 import QtGui, QtCore, QtWebKit
class Browser(QtWebKit.QWebView):
_windows = set()
#classmethod
def _removeWindow(cls, window):
if window in cls._windows:
cls._windows.remove(window)
#classmethod
def newWindow(cls):
window = cls()
cls._windows.add(window)
return window
def __init__(self, parent=None):
QtWebKit.QWebView.__init__(self, parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
self.settings().setAttribute(
QtWebKit.QWebSettings.JavascriptEnabled, True)
self.settings().setAttribute(
QtWebKit.QWebSettings.JavascriptCanOpenWindows, True)
def closeEvent(self, event):
self._removeWindow(self)
event.accept()
def createWindow(self, mode):
window = self.newWindow()
if mode == QtWebKit.QWebPage.WebModalDialog:
window.setWindowModality(QtCore.Qt.ApplicationModal)
window.show()
return window
html = """
<html>
<head>
<title>Test Page</title>
<script type="text/javascript"><!--
var url = 'https://www.google.com'
var count = 0
function newWindow() {
count += 1;
window.open(url, 'testwindow' + count, 'width=640,height=480');
}
--></script>
</head>
<body>
<input type="button" value="New Window" onclick="newWindow()" />
<p>Test Link</p>
</body>
</html>"""
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
browser = Browser()
browser.setHtml(html)
browser.show()
sys.exit(app.exec_())
(NB: It is essential that a reference is kept to any newly created windows; failure to do so will almost certainly result in segmentation faults).
EDIT:
Just to clarify:
The createWindow function is intended to handle external requests to open a new browser window (e.g. a javascript window.open() call, as in the example above). And note that createWindow is a virtual function. This means that if it is overridden in a subclass of QWebView or QWebPage, the reimplemented function can be called internally by Qt. This is obviously vital if you need your own creatWindow function to be called from javascript code.
So if you want javascript programs on web-pages to be able to create and open an instance of your browser window class, then subclass QWebView and reimplement its createWindow function (as in the example above).
The createWindow function is not required at all for simply creating and showing a QWebView (e.g. adding a new tab). A QWebView is just a container for displaying web-pages, and is created and shown just like any other widget.
I'm trying to put together a simple table that shows my model's data. I need row selection behaviour so I set:
self.setSelectionBehavior(QAbstractItemView.SelectRows)
All is good until I implement selectionChanged() which causes the redraw to get a bit confused every time a row is selected (cell's dont' seem to update their selection state). Here is some test code that causes the problem for me:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class Item( QStandardItem ):
def __init__( self, parent=None ):
super( Item, self).__init__( parent )
self.pixmap = QPixmap("colour.png")
#def data(self, role=Qt.UserRole + 1):
#'''with this method in place the cells get a checkbox and are not selectable'''
#return 'test'
class Model (QStandardItemModel):
def __init__( self, parent=None ):
super( Model, self).__init__( parent )
self.setHorizontalHeaderLabels(['a', 'b', 'c'])
self.init_data()
def init_data(self):
for row in range(0, 15):
for col in range(0, 10):
col_item = Item( '%s, %s' % (row, col) )
self.setItem(row, col, col_item)
class TableView( QTableView ):
def __init__( self, parent=None ):
super( TableView, self).__init__( parent )
model = Model()
self.setModel(model)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setSelectionMode(QAbstractItemView.ContiguousSelection)
self.setMouseTracking(True)
def selectionChanged(self, selected, deselected):
print selected
if __name__ == '__main__':
app = QApplication([])
table = TableView()
table.show()
sys.exit(app.exec_())
I'm also a bit confused about why the cell's all get a checkbox and become un-selectable if the data() method is implemented in the QStandardItem. Can somebody please help?
Cheers,
frank
You are overriding the QTableViews selectionChanged. That is (probably) used internally by the view and you prevent that. I am not sure why you would want to do that. You should use the selectionChanged signal of the selectionModel() for the view if you want to do custom things when the selection changes.
But if you insist on overriding selectionChanged on TableView at least call the parents function too, so that view could do its work:
class TableView( QTableView ):
def __init__( self, parent=None ):
super( TableView, self).__init__( parent )
model = Model()
self.setModel(model)
self.setSelectionBehavior(QAbstractItemView.SelectRows)
self.setSelectionMode(QAbstractItemView.ContiguousSelection)
self.setMouseTracking(True)
def selectionChanged(self, selected, deselected):
print selected
super(TableView, self).selectionChanged(selected, deselected)
But, you don't need to and you really shouldn't subclass a QTableView just to set a few properties. You can do that for an instance. Actually, all your sub-classes are unnecessary. You could write that code like this:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
if __name__ == '__main__':
app = QApplication([])
# create model
model = QStandardItemModel()
model.setHorizontalHeaderLabels(['a', 'b', 'c'])
# fill data
for row in range(15):
model.appendRow([QStandardItem('%d, %d' % (row, col)) for col in range(10)])
# create table view
table = QTableView()
# set parameters
table.setModel(model)
table.setSelectionBehavior(QAbstractItemView.SelectRows)
table.setSelectionMode(QAbstractItemView.ContiguousSelection)
table.setMouseTracking(True)
# show
table.show()
sys.exit(app.exec_())
As for overriding the data method for the model: data method is responsible for returning many different values depending on the role that view asks. You are always returning "test" for all of them. That is, to say the least, bad. If you want to create your own models, you should at least read about how models/views work in Qt. Official docs have a nice section about it. You can also find some decent video tutorials in the web.