Question

I need to post an Event from an thread to an QtreeView in my Mainwindow. Now to post an event we need the pointer to the Qobject (i.e. our qtreeview inside main window).

For this i have to take the pointer to my mainwindow in constructor MainWindow::MainWindow().

sharedobj.h file contains pointer to mainwindow :----

#include "ui_mainwindow.h" 

/*!
  Shared object among threads.
*/
class cntrlObj
{
public:
    cntrlObj();
    ~ctrlObj();

    /// Thread run control
    bool m_bQuit;

    /*!
        Pointer to mainwindow window
    */
    Ui::MainWindow *ui;

}

Mainwindow class contains following object :---

cntrlObj cntrlObj_obj;

Mainwindow constructor :-----

/*!
   Take Pointer to Mainwindow session window
*/

    cntrlObj_obj->ui_ptr->setupUi(this);

Please suggest is it an right way to take an pointer to Mainwindow ?

Also can i include file "ui_mainwindow.h" inside another sharedobj.h file to access namespace UI of mainwindow to declare an pointer as i have done in "sharedobj.h" file ?

Was it helpful?

Solution

You need to localize the knowledge within the Mainwindow, and post the event to it.

So:

  1. Post the event to the Mainwindow instance.

  2. Reimplement MainWindow::customEvent(...) as follows (if it's a custom QEvent, otherwise you'd reimplement event(...):

    void MainWindow::customEvent(QEvent * ev) {
      if (ev->type() == MyEventType) {
        QCoreApplication::sendEvent(ui->treeView, ev);
      }
    }
    

Alas, why on Earth would you need to send an event to a view?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top