C++ Qt GUI 更新

标签 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;
}

所以我想每 2 秒从相机线程获取图像并将它们发送到主线程(这确实发生了,所以我在 main_window 对象上有 QImage)。 然后我想把这个QImage放到cam1和cam2QLabel。我被困在这里:

首先:当我使用 setPixmap() 方法时,QLabel.width()QLabel.height() 不同>image.width()image.height()pixmap.width()pixmap.height()

第二:我无法想象QLabel。如果我执行 this->ImageLayout->addWidget(cam1) 什么都不会发生。 this->update 也没有帮助。

我应该有一个额外的 worker 来更新 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);
};

相机 Controller .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());
    }
}

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

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

最佳答案

好的,所以这里有一些设计问题:

  1. tmp_label 是在堆栈上创建的,无论如何都会在您的 displayImages 方法结束时销毁

  2. 每次收到新的相机帧时,您都在尝试使用 this->imageLayout->addWidget(this->cam1); 将 QLabel 添加回您的 UI。在构建小部件时添加一次,然后仅使用 cam1->setPixmap(...)

  3. 也许我错过了它,但我没有看到您在 QLabel 中设置图像的位置。这通常使用 QLabel::setPixmap

  4. 完成

然后:

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

关于C++ Qt GUI 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42935117/

相关文章:

c++ - mfc richedit2格式化

c++ - 在 QTabWidget 上显示/隐藏子选项卡

c++ - Qt 同时多次点击鼠标

c++ - 可以自动刷新QTableWidget吗?

c++ - 如何使用索引访问 C++ 结构属性值?

c++ - Valgrind 提示 string.append(string)

c++ - #ifndef#define 指令是什么意思

c++ - std::thread 访问从共享库加载的函数

c++ - 从 Qt 中的 QMainWindow 的构造函数启动一个新线程

c++ - 如何在Qt表项中定位QIcon?