Skip to content

Instantly share code, notes, and snippets.

/gridwidget.py Secret

Created Apr 19, 2015
Embed
What would you like to do?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
unicode_literals)
import os
import sys
# These are only needed for Python v2 but are harmless for Python v3.
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4 import QtCore, QtGui, uic
class GridWidget(QtGui.QTableWidget):
def resizeEvent(self, event):
total_width = self.viewport().width()
nb_columns = self.columnCount()
for column in range(nb_columns):
self.horizontalHeader().resizeSection(column,
total_width / nb_columns)
def posToRowCol(self, pos):
index = self.indexAt(pos)
if not index:
return None
return (index.row(), index.column())
def dropEvent(self, event):
if event.source() == self and \
(event.dropAction() == QtCore.Qt.MoveAction or
self.dragDropMode() == QtGui.QAbstractItemView.InternalMove):
destPos = self.posToRowCol(event.pos())
if destPos:
index = self.selectedIndexes()[0]
widget = self.cellWidget(index.row(), index.column())
if widget:
self.setCellWidget(destPos[0], destPos[1], widget.clone())
self.removeCellWidget(index.row(), index.column())
event.accept()
event.setDropAction(QtCore.Qt.CopyAction)
super(GridWidget, self).dropEvent(event)
class CustomWidget(QtGui.QLabel):
def __init__(self, parent=None):
super(CustomWidget, self).__init__(parent)
self.setStyleSheet('background-color: red; color: yellow;')
def clone(self):
clone = CustomWidget()
clone.setText(self.text())
return clone
TestPanelUi = uic.loadUiType(os.path.join(os.path.dirname(__file__),
'gridwidget.ui'))[0]
class TestPanel(QtGui.QMainWindow, TestPanelUi):
def __init__(self):
super(TestPanel, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.addWidget)
self.counter = 0
@QtCore.pyqtSlot()
def addWidget(self):
self.counter += 1
widget = CustomWidget(self.gridWidget)
widget.setText('Widget {}'.format(self.counter))
next_empty_cell = (-1, -1)
for row in range(self.gridWidget.rowCount()):
for column in range(self.gridWidget.columnCount()):
if not self.gridWidget.cellWidget(row, column):
next_empty_cell = (row, column)
break
if next_empty_cell != (-1, -1):
break
if next_empty_cell == (-1, -1):
self.gridWidget.insertRow(self.gridWidget.rowCount())
next_empty_cell = (self.gridWidget.rowCount() - 1, 0)
self.gridWidget.setCellWidget(next_empty_cell[0],
next_empty_cell[1], widget)
def main():
app = QtGui.QApplication(sys.argv)
panel = TestPanel()
panel.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="GridWidget" name="gridWidget">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropOverwriteMode">
<bool>false</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="rowCount">
<number>3</number>
</property>
<property name="columnCount">
<number>3</number>
</property>
<attribute name="horizontalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<row/>
<row/>
<row/>
<column/>
<column/>
<column/>
</widget>
</item>
</layout>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>GridWidget</class>
<extends>QTableWidget</extends>
<header>gridwidget</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment