Reputation: 7889
I am trying to rewrite some PyQt C++ code in python. I have done this type of syntax conversion many times, but not with a subclass example such as this. This C++ code is intended to allow you to add comboboxes to a QTableWidget header to act similar to Excel header filters. I can convert most of the logic, but I need help with the subclass syntax in python for this example. Any help is appreciated.
MyHorizontalHeader(QWidget *parent = 0) : QHeaderView(Qt::Horizontal, parent)
{
connect(this, SIGNAL(sectionResized(int, int, int)), this,
SLOT(handleSectionResized(int)));
connect(this, SIGNAL(sectionMoved(int, int, int)), this,
SLOT(handleSectionMoved(int, int, int)));
setMovable(true);
}
void showEvent(QShowEvent *e)
{
for (int i=0;i<count();i++) {
if (!boxes[i]) {
QComboBox *box = new QComboBox(this);
boxes[i] = box;
}
boxes[i]->setGeometry(sectionViewportPosition(i), 0,
sectionSize(i) - 5, height());
boxes[i]->show();
}
QHeaderView::showEvent(e);
}
void handleSectionResized(int i)
{
for (int j=visualIndex(i);j<count();j++) {
int logical = logicalIndex(j);
boxes[logical]->setGeometry(sectionViewportPosition(logical), 0,
sectionSize(logical) - 5, height());
}
}
void handleSectionMoved(int logical, int oldVisualIndex, int newVisualIndex)
{
for (int i=qMin(oldVisualIndex, newVisualIndex);i<count();i++){
int logical = logicalIndex(i);
boxes[logical]->setGeometry(sectionViewportPosition(logical), 0,
sectionSize(logical) - 5, height());
}
}
void scrollContentsBy(int dx, int dy)
{
QTableWidget::scrollContentsBy(dx, dy);
if (dx != 0)
horizHeader->fixComboPositions();
}
void fixComboPositions()
{
for (int i=0;i<count();i++)
boxes[i]->setGeometry(sectionViewportPosition(i), 0,
sectionSize(i) - 5, height());
}
This example source code originally comes from http://blog.qt.io/blog/2012/09/28/qt-support-weekly-27-widgets-on-a-header/
I hope to eventually create a custom subclass that I can promote for my QTableWidgets in "Qt Designer", whereby I can have custom QTableWidget QHeaderView with combobox filters.
Upvotes: 0
Views: 866
Reputation: 6075
Subclassing in PyQt/PySide starts like this:
class MyHorizontalHeader(QHeaderView):
def __init__(self, parent=None):
super(MyHorizontalHeader, self).__init__(Qt.Horizontal, parent)
def otherMethod(self):
...
...
The first line defines the name of the class and the potential inheritance.
The __init__
method is called when creating an instance of the class. It always needs to call the __init__
method of the class it inherits from (this is specific of PyQt/PySide), which is done with super
.
Upvotes: 2