C++ Qt GUI更新

hag*_*gor 1 c++ qt

我是Qt的新手,因此我遇到了GUI更新问题.我有2个类:ControlWidget在主线程和CameraController单独的线程中.

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    CameraController *cameraController = new CameraController;;
    ControlWidget *main_window = new ControlWidget;
    Thread getImageThread; 
    cameraController->moveToThread(&getImageThread);
    getImageThread.start();
    QTimer get_images_timer;
    QObject::connect(&get_images_timer, SIGNAL(timeout()), cameraController, SLOT(onTimerOut()), Qt::QueuedConnection);
    QObject::connect(cameraController, SIGNAL(sendLabel(QImage)), main_window, SLOT(getImage(QImage)), Qt::QueuedConnection);
    QObject::connect(&get_images_timer, SIGNAL(timeout()), main_window, SLOT(update()), Qt::QueuedConnection);
    get_images_timer.start(2000);
    app.exec();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

因此,每2秒我想从相机线程获取图像并将它们发送到主线程(动作发生,所以我QImage在main_window对象).然后我想把这个QImage放到cam1和cam2上QLabel.在这里我被困住了:

第一:当我使用setPixmap()方法QLabel.width()QLabel.height()不同,那么image.width()image.height()pixmap.width()pixmap.height().

第二:我无法想象QLabel.如果我什么this->ImageLayout->addWidget(cam1)都不做的话.this->update也没有帮助.

我有一个额外的GUI更新工作者吗?我究竟做错了什么?

源代码以获取更多信息:

CameraController.h

class CameraController : public QObject
{
    Q_OBJECT 
private: 
    CoreApi::InstanceHandle g_hApi;
    CoreApi::DeviceCollectionHandle hDeviceCollection;
    CoreApi::DeviceHandle hDevice;
    CoreApi::CameraPortHandle first_cam;
    Common::FrameHandle frame;
    QPixmap pixmap;
    QImage image;
public: 
    CameraController();
    ~CameraController();
    QLabel outLabel;
public slots:
    void onTimerOut();
signals:
    QImage sendLabel(QImage image);
};
Run Code Online (Sandbox Code Playgroud)

CameraController.cpp

CameraController::CameraController()
{
    try
    {
        this->g_hApi = CoreApi::Instance::initialize();
        this->hDeviceCollection = this->g_hApi->deviceCollection();
        this->hDevice = hDeviceCollection->device(0);
        this->first_cam = hDevice->cameraPort(0);
        first_cam->autoConfigure();
        first_cam->liveStart();
    }
    catch (GeneralException& e)
    {
    std::cout << e.what() << std::endl;
    }
}

CameraController::~CameraController()
{
}

void CameraController::onTimerOut()
{
    if (this->first_cam->liveFrameReady())
    {
        this->frame = first_cam->liveFrame();
        this->image =  QImage((uchar*)this->frame->buffer()->data(), this->frame->dataType()->width(), this->frame->dataType()->height(), QImage::Format::Format_RGB888);
        this->image = this->image.scaled(QSize(this->image.width()/10, this->image.height()/10));
        std::cout << "width = "<<this->image.width() << "height = " << this->image.height() << std::endl;
        emit sendLabel(this->image.copy());
    }
}
Run Code Online (Sandbox Code Playgroud)

ControlWidget.h

class ControlWidget :public QDialog
{
    Q_OBJECT
private:
    QGLCanvas *osCanvas;
    QGridLayout *mainLayout;
    QGridLayout *buttonLayout;
    QVBoxLayout *imageLayout, *settingsLayout;
    QHBoxLayout *controlLayout;
    QListWidget *cameraListWidget, *devicesListWidget;
    QLabel *cameraListLabel, *devicesListLabel, *cameraSettingsLabel, *fpsLabel, *shutterLabel;
    QHBoxLayout *fpsLayout, *shutterLayout;
    QLineEdit *fpsEdit, *shutterEdit;
    QPushButton *saveButton, *saveSettingButton, *applySettingsButton, *chooseFolderButton;
    QTimer* m_timer;
public:
    ControlWidget(QWidget *parent = 0);
    ~ControlWidget();
    QLabel *cam1, *cam2;
    QImage *camera_1, *camera_2;
    void createWidgets();
public slots:
    void getImage(QImage new_frame);
    void displayImages();
signals: 
    void images_loaded();
private slots:
    void onTimeout()
    {
        qDebug() << "Worker::onTimeout get called from controlWidget timer and  ?: " << QThread::currentThreadId();
    };
};
Run Code Online (Sandbox Code Playgroud)

ControlWidget.cpp

ControlWidget::ControlWidget(QWidget *parent)
{
    this->createWidgets();
    this->m_timer = new QTimer;
    connect(this->m_timer, SIGNAL(timeout()),this, SLOT(update()));
    m_timer->start(1000);
}

ControlWidget::~ControlWidget()
{
    delete this->mainLayout;
}


void ControlWidget::createWidgets() 
{
    this->imageLayout = new QVBoxLayout;
    this->cam1 = new QLabel;
    this->cam2 = new QLabel;
    this->imageLayout->addWidget(cam1);
    this->imageLayout->addWidget(cam2);
    this->setLayout(this->imageLayout);
    this->show();
}

void ControlWidget::displayImages()
{
    QLabel tmp_label ;

    std::cout << "********************************************************************************" << std::endl;
    std::cout <<"  camera height  = " <<this->camera_1->height() << "   camera width = " << this->camera_1->width() << std::endl;
    std::cout << "********************************************************************************" << std::endl;
    QPixmap tmp_pixmap = QPixmap::fromImage(this->camera_1->copy());
    std::cout << "PIXMAP WIDTH = " << tmp_pixmap.width() << "Pixmap Height = " << tmp_pixmap.height() <<std::endl;
    std::cout << "LABELWIDTH = "<< tmp_label.width() << "LabelHeight =  "<< tmp_label.height() << std::endl;
    tmp_label.setGeometry(200, 200, tmp_pixmap.width(), tmp_pixmap.height());
    tmp_label.show();
    this->cam1 = &tmp_label;
    this->cam2 = &tmp_label;
    std::cout << "CAM1 Width = " <<this->cam1->width() << std::endl;
    this->imageLayout->addWidget(this->cam1);
    this->imageLayout->addWidget(this->cam2);
}



void ControlWidget::getImage(QImage img)
{
    std::cout << "********************************************************************************" << std::endl;
    std::cout << "  img height  = " << img.height() << "   img width = " << img.width() << std::endl;
    std::cout << "********************************************************************************" << std::endl;
    this->camera_1 = &QImage(img);
    this->camera_2 = &QImage(img);
    this->displayImages();
}
Run Code Online (Sandbox Code Playgroud)

Adr*_*vat 5

好的,所以你在这里遇到一些设计问题:

  1. tmp_label在堆栈上创建,并且在displayImages方法结束时将被销毁

  2. 每次收到新相机框架时,您都会尝试将QLabel添加回用户界面this->imageLayout->addWidget(this->cam1);.在构造窗口小部件时添加它们一次,然后cam1->setPixmap(...)仅使用它.

  3. 也许我错过了,但我看不到你在QLabel中设置图像的位置.这通常是通过QLabel::setPixmap

然后:

  • update()在依赖像您这样的标准小部件时,调用不是必需的,当您设置像素图时,QLabel会自动更新
  • 您实际上不需要this->在C++中使用
  • 我不知道你Thread班级下面有什么,但是当你使用QThreads时你不需要Qt::QueuedConnection为连接传递参数,这是自动完成的
  • 要排除与如何实例化UI或如何使用布局相关的问题,请首先使用基于静态Qt Designer .ui的界面进行实验
  • 实际上,您可以使用QPixmap::save("image.jpg")或轻松测试您阅读的QPixmap和QImage的有效性QImage::save("image.jpg")