Qt Signal mapper and sending QLabel by reference and OpenCv 2

  • Hello Everybody, this is my first post, so i hope that i won't mess with the rules, also a small note, i'm a newbie in Qt, just one month old, it is very interesting ^_^

    i've two problems with my code, i want to send a QLabel by reference, so i could view the frames on it, the window that i use is mainwindow object and it is created by an action from the original mainwindow

    the second i wanna manage the GUI using a timer object, so i wanna connect the passed by reference method with the timer and here comes my problem with the Signal mapper

    i used this line @ orignFrame = cv::imread("E:/Images/Folders/robo.jpg");
    because i thought that the problem might be in the conversion from the frames to the QImage
    beside i added alot of qdebug where only hello1 and hello2 appears

    here is my .hpp
    QSignalMapper* windowLblSignal;
    cv::VideoCapture CapObjLabel;
    cv::Mat orignFrame;
    QImage qimgOriginal;
    QTimer* tmrWindowTimer;

    void waitForUser(QWidget *windowLabel);

    public slots:

    void changeWindowLabel(QWidget *InputWidget);
    void ExpTimer();


    here is my code of the .cpp

    @void BinaryImage::changeWindowLabel(QWidget *InputWidget){
    qDebug() << "Hello3";
    QLabel * windowLabel = qobject_cast<QLabel >(InputWidget);
    cv::flip(orignFrame, orignFrame,1);
    qDebug() << "Hello4";
    orignFrame = cv::imread("E:/Images/Folders/robo.jpg");
    QImage qimgOriginal((uchar
    )orignFrame.data, orignFrame.cols, orignFrame.rows, orignFrame.step, QImage::Format_RGB888);
    qDebug() << "Hello5";

    void BinaryImage::ExpTimer(){

    qDebug() << "Hellotimer";


    void BinaryImage::waitForUser(QWidget *windowLabel){
    tmrWindowTimer = new QTimer();
    windowLblSignal = new QSignalMapper();
    qDebug() << "Hello1";

    connect(tmrWindowTimer, SIGNAL(timeout()), windowLblSignal, SLOT(map()));
    connect(tmrWindowTimer, SIGNAL(timeout()),this, SLOT(ExpTimer()));
    connect(windowLblSignal, SIGNAL(mapped(QWidget*)), this, SLOT(changeWindowLabel(QWidget*)));
    windowLblSignal->setMapping(windowLblSignal, windowLabel);
    qDebug() << "Hello2";
    tmrWindowTimer -> start();


    and for the ui class
    NewValues::NewValues(QWidget *parent) :
    ui(new Ui::NewValues)

    Statlabel = new QLabel(this);


    delete ui;

    void NewValues::on_btnStart_clicked()
    BinaryImage mBinaryImage;

        // Show the user the values he collected
    mcnfvals = new ConfirmValues(this);


    the window of ConfirmValues appear and the qdebug or Hello3 , Hello4 and Hello5 never show up

  • also i forgot to mention that somebody from another site told me "Use
    @QSignalMapper::setMapping(QObject *sender, QWidget *widget) @
    @ QSignalMapper::mapped(QWidget *widget) @
    signal, then cast it to
    by using
    @qobject_cast@ "
    i think that this is what i did but i'm not sure if i did it the right way.

    i don't know if the problem so complicated or it is so easy and i'm dull, but if there is anyone who can help, please say your opinion cause i'm struggling in this issue !!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It looks like you are trying to do many things at the same time with the same class i.e. a recipe for catastrophe and maintenance nightmare.

    If I understood things correctly, BinaryImage is there to read something with OpenCV and provide a QPixmap from it's content. It should not care on what widget you want to put that QPixmap. Just make it emit a signal providing the new pixmap. Your MainWindow should handle the connection to the label.

    If you need some input from the user, use a QDialog. Depending on what you need from them QInputDialog might already provide what you need. Again, think carefully about whether it's the roll of BinaryImage to do that.

    Hope it helps

  • thanks SGaist, you understood very well, and Binary Image Class has alot of roles to do, alot of caluclations and anding operation between filtered images, i'll try to look at the QInputDialog.
    until that, do you see anything might make an issue in the code up there for the Qtimer not to call on the SLOT as

    @the qDebug()<< "Hello3" @

    doesn't run !!

  • Lifetime Qt Champion

    void NewValues::on_btnStart_clicked()
    BinaryImage mBinaryImage;

        // Show the user the values he collected
    // this here is a memory eater since you never destroy it (it will be at program's end but until there…)
    mcnfvals = new ConfirmValues(this);

    } <- mBinaryImage is destroyed here

  • yeah, because i'll destroy it when it opens cause it have a button to ask me if i agree on the values, and when i press confirm, it will close))
    but i'm sorry, i guess i've another issue with the Timer ?!
    and the SignalMapper parameters ?!

  • also i'd a review the QInputDialog, but that's not what i need, actually the
    frame that i'll show to the user i'll also have a copy from it to make
    processing on it.

    Kindly help me to pass my two initial problem of sending a QLabel by
    reference and use it as a parameter in a SLOT when a timer's timeout signal

    also there was an issue in the previous code, that qDebug<<"Hello3"; doesn't
    get called

  • Lifetime Qt Champion

    Can you describe the workflow of that functionality ? e.g. User click on button, show dialog with image from source, etc… What will the user input change ?What you want to do with the timer ?

  • Okay,.. i'll explain
    there is a Qmainwindow that open when i press on an action in the menu bar of the original mainwindow, there is a button says Start and a QLabel to view image - there will be a label for status bar but not implemented yet - user click the button, a timer begin to count, when it times out, the connect - i'll get straight to explaining the SLOT - will activate a method that takes a frame and draw some boxes using Opencv methods, then convert this frame to QImage and show it on the label.
    click signal - > activate timer -> call slot -> query frame -> view it on a passed by reference label.
    this is the first part in my project, i already finished it in Visual studio, but Qt is much more interesting, there is python and c++ ♥
    i'm grateful for all the help here))))

  • Lifetime Qt Champion

    Then my earlier suggestions still stand. Keep the image processing in BinaryImage and emit the QPixmap when ready. It really doesn't need know about that QLabel.

  • but how can i update the label, and if the QPixmap will be the return type of a method, how would i keep calling it in a SLOT which is void ?!
    i use the timer to manage the GUI, cause i don't want things to happen fast and make the GUI not responding

  • Lifetime Qt Champion

    I didn't suggest to make QPixmap the return type of anything. Use a signal that looks like:

    void newPixmapReady(const QPixmap& pixmap);

    If you don't want to lock your GUI thread, you can get some inspiration from the Mandelbrot example

  • as far as i know that "connect" only accept signal and slot with the same parameters, but there is no timeout() with QPixmap ?!
    that's why i use SignalMapper and it doesn't work at all for me, i feel that i've a misunderstanding about the SignalMapper xD

    also none of the connect statements that i wrote in the code have worked

  • Lifetime Qt Champion

    It's more about the design. Your timer should be connected to that slot that does the processing, then from this slot emit a signal with the QPixmap

  • but still timers doesn't work in the code up there, is there some problem in they way i made them ?!

  • Lifetime Qt Champion

    You should really first cleanup the code. Using a QSignalMapper for that task is just an overkill.

    Anyway, one of the thing is that you are mapping the mapper to the widget which is not the way it's supposed to work. Have a look at QSignalMapper's documentation, there's an example of how it works.

  • yeah, you're right, sorry for making it too long, any technical design recommendation about things i can have a look at else the Mandelbrot example ?!

  • Lifetime Qt Champion

    The documentation (Qt 5 latest version) of QThread, you'll see the various implementation possibilities. Then don't try to do everything at once. First get your image on the widget, then move that code to a worker object. Also, since you want to do it at regular interval, why not simply use a QTimer ?

    Hope it helps

  • yup, thanks, i'm reading a book about QT Design Patterns, i think i should learn it in a better way

  • Lifetime Qt Champion

    Good book, there are several that you can learn from

Log in to reply