[英]C++ Qt GUI update
我是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和cam2 QLabel
。 我被困在这里:
首先:当我使用setPixmap()
方法时, QLabel.width()
和QLabel.height()
是不同的,然后image.width()
image.height()
和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);
};
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());
}
}
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();
};
};
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();
}
好的,所以这里有一些设计问题:
tmp_label
在堆栈上创建,并且无论如何都会在displayImages
方法的末尾销毁
每次收到新的相机框架时,您都尝试使用this->imageLayout->addWidget(this->cam1);
将QLabels添加回您的UI this->imageLayout->addWidget(this->cam1);
。 而是在构造窗口小部件时添加一次,然后仅使用cam1->setPixmap(...)
。
也许我错过了,但是我看不到您在QLabel中设置图像的位置。 这通常是通过QLabel::setPixmap
接着:
update()
,QLabel在设置其像素图时会自动更新 this->
Thread
类下有什么内容,但是当使用QThreads时,您不需要为连接传递Qt::QueuedConnection
参数,这是自动完成的 QPixmap::save("image.jpg")
或QImage::save("image.jpg")
轻松测试所读取的QPixmap和QImage的有效性。 除了@basslo注释之外,请勿执行此操作- this->cam1 = &tmp_label;
。 当tmp_label
被销毁(它是一个局部变量)时,它也将从其所属的布局中删除,因此它永远不会被实际显示。
使用this->cam1->setPixmap(...)
代替分配新图像,并定义大小策略,以从构造扩展到扩展( 此答案提供了有关它的更多信息)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.