Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. calling from external function
Forum Updated to NodeBB v4.3 + New Features

calling from external function

Scheduled Pinned Locked Moved General and Desktop
singleton classqt5.5
8 Posts 2 Posters 3.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mehdidante
    wrote on last edited by
    #1

    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_OBJECT

    public:

    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 ) .

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        M Offline
        M Offline
        mehdidante
        wrote on last edited by
        #3

        @SGaist tnx for replay . i see . i kind of guessed that is the problem . so how can i access my qt_testi from my test() function? . how to fix this? . that should be possible .

        M 1 Reply Last reply
        0
        • M mehdidante

          @SGaist tnx for replay . i see . i kind of guessed that is the problem . so how can i access my qt_testi from my test() function? . how to fix this? . that should be possible .

          M Offline
          M Offline
          mehdidante
          wrote on last edited by
          #4

          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 ).

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mehdidante
            wrote on last edited by
            #5

            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 ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Make the constructor private so you can only create it when you call instance the first time. No need for that QApplication hack.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mehdidante
                wrote on last edited by
                #7

                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 .

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Search for "singleton pattern" for the correct implementation and usage. It's not just a question of having a static pointer to an instance.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved