QWidget*widget1 = new QWidget(this); widget1.setMinimumSize(400,400); QPushButton but1("这是一个按钮",&widget1);,報錯,怎麽修改
时间: 2024-04-09 19:33:58 浏览: 137
这里报错是因为 `QPushButton` 的父对象应该是指针类型,而不是对象本身。你可以将父对象指针传递给 `QPushButton` 的构造函数。下面是修改后的代码:
```cpp
QWidget* widget1 = new QWidget(this);
widget1->setMinimumSize(400, 400);
QPushButton* but1 = new QPushButton("这是一个按钮", widget1);
```
这样就能够正确地创建一个带有父对象的按钮了。
相关问题
QWidget*widget2 = new QWidget(this); widget2->setMinimumSize(400, 400); QPushButton* but2 = new QPushButton("这是第二个按钮", widget1); QStackedLayout *stackedLayout = new QStackedLayout; stackedLayout->addWidget(&widget1); stackedLayout->addWidget(&widget2);,報錯,爲啥
这里报错是因为 `addWidget` 方法需要传递指针类型的参数,而你传递的是对象本身的地址。修改代码如下:
```cpp
QStackedLayout *stackedLayout = new QStackedLayout;
stackedLayout->addWidget(widget1);
stackedLayout->addWidget(widget2);
```
这样就能够正确地向 `QStackedLayout` 中添加 `QWidget` 对象了。
如何将以下代码转成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(); }
以下是将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`中的模块。
阅读全文