calling from external function
-
hi .
i have a function ( which i can not make member of main class) and i should change my ui ( change the background of a label ) . so i'm using singleton and signal and slots to achive my goal . code seems to be ok but it doeasnt work .
i use qt 5.5.0 and visual studio 2013 ( i'm also using opencv but its irrelevent because i tested lable->setText() and it didnt worked ) .
this is my header file :#ifndef QT_TESTI_H
#define QT_TESTI_H#include <QtWidgets/QMainWindow>
#include "ui_qt_testi.h"
#include <opencv2/opencv.hpp>using namespace cv;
Q_DECLARE_METATYPE(Mat);
class qt_testi : public QMainWindow
{
Q_OBJECTpublic:
qt_testi(QWidget *parent = 0); ~qt_testi(); static qt_testi *instance() { if (!pointer) pointer = new qt_testi; return pointer; }
signals:
void updateUI(Mat &img);private slots:
void on_btnShow_clicked();
void setLblBgd(Mat &img);private:
Ui::qt_testiClass ui;
static qt_testi *pointer;
};#endif // QT_TESTI_H
and this is my cpp file :
#include "qt_testi.h"
//#include <iostream>
//#include <string>
#include <qmessagebox.h>
#include <opencv2/opencv.hpp>using namespace std;
using namespace cv;qt_testi* qt_testi::pointer = NULL;
qt_testi::qt_testi(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
qRegisterMetaType<Mat>("Mat");connect(this, SIGNAL(updateUI(Mat&)), SLOT(setLblBgd(Mat&)));
}
void qt_testi::setLblBgd(Mat &img)
{
QImage image = QImage((uchar*)img.data, img.cols, img.rows, img.step1(), QImage::Format::Format_RGB888);
QPixmap pix = QPixmap::fromImage(image);
ui.lbl->setScaledContents(true);
ui.lbl->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
ui.lbl->setPixmap(pix);
}void test()
{
qt_testi* t = qt_testi::instance();
// reading an image from hard and store it in a opencv object
Mat img = imread("image.jpg", CV_LOAD_IMAGE_COLOR); format(Mat)
emit t->updateUI(img);
}qt_testi::~qt_testi()
{
}void qt_testi::on_btnShow_clicked()
{
//Mat img = imread("image.jpg", CV_LOAD_IMAGE_COLOR);
//emit updateUI(img);
test();
}so as u see in my button click i call a function named test() . in test i'm creating new instance and try to emit a signal . i debugged, and the signal is correctly catches and i can see my image in Image Watch . but it seems that ui->lbl->setPixmap() doesn't work because nothing happens .
if i uncomment my first two lins in button click and comment out the line test() , everything works fine and the label background sets correctly .
please help . it's a silent problem ( i have no error ) . -
Hi and welcome to devnet,
If I understood you correctly, you currently have two qt_testi objects. One that you create the usual way and the other that you create the first time you call instance. So what is happening is that you are updating the second invisible widget.
-
ok . so with your help i was able to make that correct . instead of using new , i created a pointer to my original class by using qapp . that works . so this is my new code :
// i've also changed the name of function from instance to getPointer
static qt_testi* getPointer()
{
pointer = (qt_testi*)qApp->activeWindow();
return pointer;
}
( iknow this is C style and i'm gonna change that to c++ cast style but it works for now ). -
hi . again . well the solution i mentioned was permenant . it works as far as u have one windows but if u have more or u are using a QFileDialog (for exp.) then your active windows will change and this does not work anymore . is there a way to point my pointer to a specific form ?
-
Make the constructor private so you can only create it when you call instance the first time. No need for that QApplication hack.
-
hi . i can't quit understand ( which constructor ? if not using qApp how can i point my pointer ) .
but there is something i wanna say :
well the signal ( updateUi ) is in qt_vlc5 class . i'm trying to call that signal from a function which is not a member of qt_vlc5 ( test ) . i need test function to access it . ( i've done some search and it looks like so many people have the similar problem ) . in Qt everything is happening in its namespaces and classes and u can not make a relationship to other non member functions . -
Search for "singleton pattern" for the correct implementation and usage. It's not just a question of having a static pointer to an instance.