Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在child';从绘画事件看中国_C++_Qt - Fatal编程技术网

C++ 如何在child';从绘画事件看中国

C++ 如何在child';从绘画事件看中国,c++,qt,C++,Qt,我是中国人,英语很差,所以我用代码来表达我自己 class Widget:public QWidget { private: //As a child object of Widget object QLabel* label; }; // Override the paintEvent void Widget::paintEvent(QPaintEvent* event) { //How to draw a picture in the QLabel not in the

我是中国人,英语很差,所以我用代码来表达我自己

class Widget:public QWidget 
{
private:
//As a child object of Widget object
    QLabel* label; 

};

// Override the paintEvent
void Widget::paintEvent(QPaintEvent* event)
{
 //How to  draw a picture in the QLabel not in the full  Widget's view,thx!!
}

首先,您需要在构造函数中初始化标签,添加如下内容:

void Widget::Widget()
{
   label=new QLabel(this);
}
然后你的绘画活动会做这样的事情-

void Widget::paintEvent(QPaintEvent* event)
{
    // create QPixmap or QImage object or how else you store it and load it to label
    QImage img("./myimage.jpg");
    label->setPixmap(img.pixmap());
}

它将绘制在标签上,但默认情况下,标签的大小会越来越大-如果小部件没有任何其他对象,它将调整大小以尽可能多地填充空间-标签将占据整个空间,因此最好将QLabel添加到一些布局中(
QGridLayout
)您需要
设置QLabel的izePolicy
QSizePolicy::Maximum

已经更新了答案,它的布局和大小策略让您困惑,我已经解决了!!!!非常感谢你。我几乎用了一天的时间来解决它!!!没有你的帮助,我会继续思考……不客气,你很久以前也有过同样的问题,但仍然记得挫折:)