无法复制QLabel的像素图

时间:2015-06-06 16:49:56

标签: c++ qt refresh qpixmap qlabel

我已经被困在这个问题上2天了。我在Qt上使用Visual Studio 2013的{​​{1}}插件。

我一直试图在Window 7-64 bit s中显示一对图像。我需要定期操作像素数据,因此我将它们存储在QLabel s中,每次我想刷新显示时,我都会设置QImage的{​​{1}}。问题是,如果我以某种方式更改/移动窗口,它似乎只会刷新。

如果我只是制作QPixmap的{​​{1}}个孩子,但从未设置布局,则问题就会消失。如果我然后添加QLabelQLabel,则会出现问题。

(这是我使用QWidget发布的一个非常相似的帖子,但问题似乎比这更重要,所以我重新发布)

这是我的代码。首先是.h

repaint()

和.cpp。

update()

在这里失去理智。我对QGraphicsScene的经验非常有限,但我相信我有正确的方法。

请帮忙!

2 个答案:

答案 0 :(得分:0)

我在Ubuntu上测试了你的代码。遗憾的是我不会评论Windows。我稍微修改了代码(参见// @ w:标记的注释):

#include "displaywidget.h"

DisplayWidget::DisplayWidget(int width, int height, QWidget *parent): QWidget(parent)
{
    w = width;
    h = height;

    CreateWidgets();
    SetupGui();

    // seed the random number generator
    srand(RAND_SEED);
    GenerateNewData();
}

DisplayWidget::~DisplayWidget()
{
}

void DisplayWidget::CreateWidgets()
{
    bImage = new QImage(w, h, QImage::Format_ARGB32);
    dImage = new QImage(w, h, QImage::Format_ARGB32);

    bimageLabel = new QLabel(this);
    bimageLabel->setScaledContents(true);

    dimageLabel = new QLabel(this);
    dimageLabel->setScaledContents(true);

    debugButton = new QPushButton("DEBUG", this);
    bimageLabel->setStyleSheet("QLabel {background-color: black};");
    dimageLabel->setStyleSheet("QLabel {background-color: grey};");

    frameGrab = new QTimer(this);
}

void DisplayWidget::SetupGui()
{
    //@w: Adding vertical layout as button below seems like better usage of space...
    QVBoxLayout * vlay = new QVBoxLayout();

    QHBoxLayout * hlay = new QHBoxLayout();
    hlay->addWidget(bimageLabel);
    hlay->addWidget(dimageLabel);
    vlay->addLayout(hlay);
    vlay->addWidget(debugButton);

    //@w: Removing size constraints on top layout allows me to resize window and see effect.
    vlay->setSizeConstraint(QLayout::SetNoConstraint);
    setLayout(vlay); // commenting this line out makes it refresh

    connect(frameGrab, SIGNAL(timeout()),this, SLOT(GenerateNewData()));
    //@w: I suppose we can chuck the button.... Currently it serves no purpose
    connect(debugButton, SIGNAL(clicked()), this, SLOT(GenerateNewData()));

    //@w: Timer slower initially, then increase to see where performance degrades.
    frameGrab->start(200);
}

void DisplayWidget::GenerateNewData()
{
    QRgb * bImageData = (QRgb *)bImage->scanLine(0);
    QRgb * dImageData = (QRgb *)dImage->scanLine(0);

    //@w: This code just varies the contents by having a b and d selector that
    //    alternates colour... Simple stuff...
    int bSelect = rand() % 3,
        dSelect = (bSelect==2) ? 0 : bSelect+1;

    for (int i = 0; i < w * h; i++)
    {
        //@w: 3 colours, only two being selected - can be improved, I suppose.
        QRgb rgb[3] =
        {
          (bSelect == 0) || (dSelect==0) ? qRgba(rand() % FULLSCALE, 0, 0, FULLSCALE) : 0,
          (bSelect == 1) || (dSelect==1) ? qRgba(0, rand() % FULLSCALE, 0, FULLSCALE) : 0,
          (bSelect == 2) || (dSelect==2) ? qRgba(0, 0, rand() % FULLSCALE, FULLSCALE) : 0,
        };

        bImageData[i] = rgb[bSelect];
        dImageData[i] = rgb[dSelect];
    }

    //@w: Removed scaling, as it depends on layout...
    bimageLabel->setPixmap(QPixmap::fromImage(*bImage));
    dimageLabel->setPixmap(QPixmap::fromImage(*dImage));
}

最初,标签从pixmaps中获取其大小。此后,标签遵循布局,这符合表单(主/父窗口小部件)的大小调整

答案 1 :(得分:0)

QtForum为我解决了这个,男孩很尴尬。

我忘了初始化循环计数器。修正了一切。