C++ Can';不要将我的小部件添加到布局中
为什么我不能将我的小部件添加到布局中?这就像添加小部件的方法一样。。。 我的目标是绘制一个小的QWidget,其目的只是保存绘制的元素C++ Can';不要将我的小部件添加到布局中,c++,qt,layout,C++,Qt,Layout,为什么我不能将我的小部件添加到布局中?这就像添加小部件的方法一样。。。 我的目标是绘制一个小的QWidget,其目的只是保存绘制的元素 void Note::paintEvent(QPaintEvent *event) { paintExample = new(QWidget); paintExample->setGeometry(500,500,500,500); paintExample->setStyleSheet("background:yellow
void Note::paintEvent(QPaintEvent *event)
{
paintExample = new(QWidget);
paintExample->setGeometry(500,500,500,500);
paintExample->setStyleSheet("background:yellow");
QImage *arrowImg = new QImage(100,100,QImage::Format_RGB32);
QHBoxLayout *lay = new QHBoxLayout;
lay->addWidget(arrowImg); //Error: no matching function for call to 'QHBoxLayout::addWidget(QImage*&)'
paintExample->setLayout(lay);
QPainter painter(arrowImg); //Must paint on QImage/QPixmap/QPicture, QWidget not possible?
if (painter.isActive()){
painter.begin(arrowImg);
painter.setPen(Qt::black);
QRect rect = QRect(50,25,60,40);
painter.drawRect(rect);
painter.end();
}
paintExample->show();
}
在专用区域的类标题中:
QWidget * paintExample;
为什么我不能将我的小部件添加到布局中
因为QImage
不是QWidget
。这就是为什么。您可能应该将图像包装在QLabel
中:
QLabel arrowLabel;
arrowLabel.setPixmap(QPixmap::fromImage(*arrowImg));
并将其传递到布局:
lay->addWidget(arrowLabel);
仔细阅读文档:
- ,以及任何其他布局,只能保存从继承的项,即QLayout本身、QSpacerItem和QWidgetItem。创建QWidget并在其上绘制
- 构造函数接受来自的子体,QWidget就是其中之一,所以这是完全可能的
- 您可以在
中创建无父对象,而不删除它们。不仅这种方法可以被频繁调用,这种绘画方法一点也不好。据我所知,paintEvent()
已经是一个QWidget了,所以你可以用Note
QPainter painter(这个)代码>
- 使用布局的操作绝不能在
中完成,也不能显示/隐藏小部件。在其他地方执行此操作,例如在窗口/对话框构造函数中李>paintEvent()
调用'QPixmap::frommage(QImage*&)'arrowLabel.setPixmap(QPixmap::frommage(arrowImg)),没有匹配的函数^
@user2366975,它是QPixmap::fromImage(*arrowhimg)
。我制作这些东西是因为我想在Note小部件和另一个用作菜单的小部件之间画一个箭头。箭头应放在便笺上方,但应放在菜单下方,这样箭头看起来似乎属于菜单的窗口装饰。所以我不能画画来记录…我怎么能画画来记录,这不是QPicture,也不是QImage,也不是Qpixmap你没有听说过什么。QPaint构造函数接受QWidget*
作为其参数,因为QWidget继承了QPaint设备。