如何将以下代码转成pyside6的 #include <QtGui> class GripLabel: public QLabel { Q_OBJECT public: GripLabel(QString const& title, QWidget* parent = 0) : QLabel(title, parent), resizing(false), gripSize(10, 10) { // Prevent the widget from disappearing altogether // Bare minimum would be gripSize setMinimumSize(100, 30); } QSize sizeHint() const { return minimumSize(); } protected: bool mouseInGrip(QPoint mousePos) { // "handle" is in the lower right hand corner return ((mousePos.x() > (width() - gripSize.width())) && (mousePos.y() > (height() - gripSize.height()))); } void mousePressEvent(QMouseEvent *e) { // Check if we hit the grip handle if (mouseInGrip(e->pos())) { oldPos = e->pos(); resizing = true; } else { resizing = false; } } void mouseMoveEvent(QMouseEvent *e) { if (resizing) { // adapt the widget size based on mouse movement QPoint delta = e->pos() - oldPos; oldPos = e->pos(); setMinimumSize(width()+delta.x(), height()+delta.y()); updateGeometry(); } } void paintEvent(QPaintEvent *e) { QLabel::paintEvent(e); QPainter p(this); p.setPen(Qt::red); p.drawRect(width()-gripSize.width(), height()-gripSize.height(), gripSize.width(), gripSize.height()); } private: bool resizing; QSize gripSize; QPoint oldPos; }; sample 主要: #include "griplabel.h" int main(int argc, char **argv) { QApplication app(argc, argv); QWidget *w = new QWidget; QPushButton *b = new QPushButton("button"); GripLabel *l = new GripLabel("Hello"); QHBoxLayout *y = new QHBoxLayout; y->addWidget(b); y->addWidget(l); y->setSizeConstraint(QLayout::SetFixedSize); w->setLayout(y); w->show(); return app.exec(); }
时间: 2024-01-25 17:02:27 浏览: 95
PySide2出现“ImportError: DLL load failed: 找不到指定的模块”的问题及解决方法
以下是将C++代码转换为使用Pyside6的Python代码:
```python
from PySide6.QtCore import QPoint, QSize
from PySide6.QtGui import QPainter
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QLabel
class GripLabel(QLabel):
def __init__(self, title: str, parent=None):
super().__init__(title, parent)
self.resizing = False
self.gripSize = QSize(10, 10)
self.setMinimumSize(QSize(100, 30))
def sizeHint(self):
return self.minimumSize()
def mouseInGrip(self, mousePos: QPoint):
return mousePos.x() > (self.width() - self.gripSize.width()) and mousePos.y() > (self.height() - self.gripSize.height())
def mousePressEvent(self, e):
if self.mouseInGrip(e.pos()):
self.oldPos = e.pos()
self.resizing = True
else:
self.resizing = False
def mouseMoveEvent(self, e):
if self.resizing:
delta = e.pos() - self.oldPos
self.oldPos = e.pos()
self.setMinimumSize(self.width() + delta.x(), self.height() + delta.y())
self.updateGeometry()
def paintEvent(self, e):
super().paintEvent(e)
p = QPainter(self)
p.setPen(Qt.red)
p.drawRect(self.width() - self.gripSize.width(), self.height() - self.gripSize.height(), self.gripSize.width(), self.gripSize.height())
if __name__ == '__main__':
app = QApplication([])
w = QWidget()
b = QPushButton("button")
l = GripLabel("Hello")
y = QHBoxLayout()
y.addWidget(b)
y.addWidget(l)
y.setSizeConstraint(QLayout.SetFixedSize)
w.setLayout(y)
w.show()
app.exec()
```
需要注意的是,由于Pyside6和PyQt5在某些方面具有不同的语法和实现,因此在转换代码时可能需要进行一些调整。在本例中,我对`QLayout`类的引用进行了一些调整,并将`QPoint`和`QSize`类的头文件引用替换为`PySide6.QtCore`中的模块。
阅读全文