简体   繁体   English

无法将我的小部件添加到布局

[英]Can't add my Widget to a layout

Why can't I add my widget to the layout? 为什么不能将小部件添加到布局? It is like the example for adding Widgets... My aim is to draw into a small QWidget, whichs purpose is only to hold that painted element. 就像添加小部件的示例一样。我的目标是绘制成一个小的QWidget,其目的只是保留该绘制的元素。

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();
}

In the class header in private area: 在私有区域的类头中:

QWidget * paintExample;

Read the documentation carefully: 仔细阅读文档:

  • QHBoxLayout , and any other layout, could only hold items inheriting from QLayoutItem , which are QLayout itself, QSpacerItem and QWidgetItem. QHBoxLayout和任何其他布局只能容纳从QLayoutItem继承的项目,这些项目是QLayout本身,QSpacerItem和QWidgetItem。 Create QWidget and paint on that. 创建QWidget并在其上绘画。
  • QPainter constructor accepts descendants from QPaintDevice and QWidget is among those, so it's totally possible. QPainter构造函数接受QPaintDevice的后代,而QWidget就在其中,因此这是完全可能的。

Now, to other issues: 现在,要解决其他问题:

  • You create parentless objects in paintEvent() without deleting them. 您可以在paintEvent()创建无父母对象,而不删除它们。 Not only this method could be called quite frequently, this approach to painting is not good at all. 这种方法不仅可以被频繁调用,而且这种绘画方法也不是很好。 As I understand Note is a QWidget already, so you're free to paint on it right away, with QPainter painter(this); 据我了解, Note已经是一个QWidget,因此您可以立即使用QPainter painter(this);在其上进行绘画QPainter painter(this); .
  • Operations with layout are most definitely not meant to be done in paintEvent() either, as well as showing/hiding widgets. 绝对不打算在paintEvent()进行带有布局的操作,以及显示/隐藏小部件。 Do this somewhere else, eg in your window/dialog constructor. 在其他地方执行此操作,例如在窗口/对话框构造函数中。

Why can't I add my widget to the layout? 为什么不能将小部件添加到布局?

Because QImage is not a QWidget . 因为QImage不是QWidget That's why. 这就是为什么。 You should probably wrap the image in a QLabel : 您可能应该将图像包装在QLabel

QLabel arrowLabel;
arrowLabel.setPixmap(QPixmap::fromImage(*arrowImg));

and pass that to the layout: 并将其传递给布局:

lay->addWidget(arrowLabel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将动态小部件添加到 QT 中的小部件布局中 - How can i add dynamically widget into a widget's layout in QT 在MainWindow中看不到我的标签和布局 - Can't see my label and layout in MainWindow 动态地将自定义窗口小部件添加到嵌入式布局 - Dynamically add a custom widget to a baked-in layout 如果有可用的位置,如何在布局中添加小部件? 否则不加 - How to add widget in layout if there is available place for it? Otherwise not to add 当我使用 Qt/C++ 将其他小部件添加到基本小部件时,如何增加基本小部件和所属垂直布局的高度? - How can I increase the height of a basic-widget and a belonging vertical layout when I add other widgets to the basic-widget using Qt/C++? 如何动态地将小部件(例如QPushButton)添加到设计器内置的布局中 - How to add a widget (QPushButton for example) dynamically to a layout built in designer QScrollArea在我的自定义小部件中不起作用 - QScrollArea doesn't work in my custom widget 如何切换网格布局中的窗口小部件可见性? - How can i toggle a widget visibility that is in a grid layout? 无法从Qt中的另一个窗口小部件访问窗口小部件中的控件 - Can't access a control in a widget from another widget in Qt 为什么我不能在我的矢量中添加项目? - Why can't I add items into my vector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM