Results 1 to 5 of 5

Thread: Qt/Code Design Problem - calling object from another object.

  1. #1
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Qt/Code Design Problem - calling object from another object.

    Hi,

    I have a code design problem:

    I have several classes
    *MainWindow - holds 2 widgets, a QTableView Widget and a QTabWidget
    *TabLayout - layout of the tab, comboboxes etc. Creates an instance of TabCode and connects its signals to it.
    *VideoTab - contains the slots. Slots are called whenever a combobox is activated.
    *SQLconnection - contains static functions to populate the combobox's options.


    The program has two parts - a SQL table and a collection of tabbed options.
    When a user selects an option from the tab's combobox, the SQL table's query needs to be changed to reflect the user's selection.

    The GUI looks like this at the moment:
    screenShot1.jpg


    What I need is to be able to communicate from the VideoTab object to the MainWindow object whenever the user selects a combobox/whenever a slot is activated. I've attached a diagram to illustrate my ideas:
    diagram.jpg



    I've tried using a static method, but that would mean that I would have to also make QSQLQueryModel static in order to modify its setQuery() function. Here's the MainWindow code for reference:

    Qt Code:
    1. #include <QtGui>
    2. #include <QMessageBox>
    3. #include <QString>
    4. #include <QtSQL>
    5. #include <QVector>
    6. #include <QString>
    7. #include <QSqlTableModel>
    8. #include "MainWindow.h"
    9. #include "VideoTab.h"
    10. using namespace std;
    11.  
    12.  
    13.  
    14. MainWindow::MainWindow(){
    15. this->db = QSqlDatabase::addDatabase("QSQLITE", "myConnection");
    16. db.setDatabaseName("myDataBase.sqlite");
    17. db.open();
    18. createDisplay();
    19.  
    20. }
    21.  
    22.  
    23.  
    24.  
    25. void MainWindow::createDisplay(){
    26. createModelView();
    27. QTableView *view = new QTableView();
    28. view->setModel(model);
    29. view->show();
    30.  
    31. createTabView();
    32. QHBoxLayout *mainLayout = new QHBoxLayout();
    33. mainLayout->addWidget(view);
    34. mainLayout->addWidget(tabWidget);
    35. this->setLayout(mainLayout);
    36.  
    37.  
    38. setWindowTitle(tr("Menus"));
    39. setMinimumSize(160, 160);
    40. resize(1000, 400);
    41.  
    42. }
    43.  
    44.  
    45.  
    46.  
    47.  
    48. void MainWindow::createModelView(){
    49. this->model = new QSqlQueryModel();
    50. model->setHeaderData(0, Qt::Horizontal, tr(""));
    51. model->setHeaderData(1, Qt::Horizontal, tr(""));
    52. model->setQuery("Select * from video", db);
    53. }
    54.  
    55.  
    56.  
    57.  
    58. void MainWindow::createTabView(){
    59. videoTab = new VideoTab;
    60. tabWidget = new QTabWidget;
    61. tabWidget->addTab((videoTab), tr("Video"));
    62. //tabWidget->addTab(audioTab = new AudioTab, tr("Audio"));
    63. //tabWidget->addTab(transportTab = new TransportStreamTab, tr("Transport Stream"));
    64. }
    To copy to clipboard, switch view to plain text mode 

    I hope I've clear stated my problem. I look forward to any comments or suggestion you peeps my have.

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qt/Code Design Problem - calling object from another object.

    It all looks like you want have a look at the signal and slot mechanism of Qt: http://doc.trolltech.com/latest/signalsandslots.html

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qt/Code Design Problem - calling object from another object.

    ... as a pseudo code:
    Qt Code:
    1. // in VideoTab
    2. Q_EMIT mySignalWhichShouldDoSomethingInMainWindow();
    3. // in TabLayout you do: (assuming you have a backpointer to your MainWindow:
    4. connect(videoTab, SIGNAL(mySignalWhichShouldDoSomethingInMainWindow()),
    5. backPointerToMainWindow, SLOT(doSomething());
    6. // and in main window:
    7. void MainWindow::doSomething()
    8. {
    9. // something in VideoTab has changed.
    10. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Qt/Code Design Problem - calling object from another object.

    I've tried creating a signal/slots. Something like:

    Qt Code:
    1. connect(vTypeCombo, SIGNAL(activated(const QString)), this->parent(), SLOT(videoUpdate()));
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. connect(vTypeCombo, SIGNAL(activated(const QString)), this->parent(), SLOT(MainWindow::videoUpdate()));
    To copy to clipboard, switch view to plain text mode 
    where videoUpdate() is a public slot. However, I get no response from the slot (nor error message).

    With a backPointerToMainWindow, do you mean:

    Qt Code:
    1. MainWindow *backPointerToMainWindow = this; //memory address of MainWindow object? Correct?
    2. or
    3. MainWindow *backPointerToMainWindow = *this; //'value' of memory address, i.e. MainWindow object? Correct?
    To copy to clipboard, switch view to plain text mode 
    And then pass this pointer to the VideoTab object's constructor?

    Thanks for your help. I'm still learning C++ and all of it's pointer/references related material.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Qt/Code Design Problem - calling object from another object.

    the parent() function is useless since you only get a pointer to an QObject not to your actual class, so you have to cast it. Or simply do something like
    Qt Code:
    1. //in class A
    2. B* b = new B(this);
    3. // in class B
    4. B::B(A* a)
    5. {
    6. C* c = new C;
    7. connect(c, SIGNAL(/*...*/), a, SLOT(/*...*/));
    8. // or
    9. A* a = qobject_cast<A*>(this->parent());
    10. //...
    11. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 10
    Last Post: 2nd December 2010, 07:10
  2. Calling object's operator+ from QtScript
    By PolyVox in forum Qt Programming
    Replies: 1
    Last Post: 1st July 2010, 22:37
  3. Replies: 7
    Last Post: 15th January 2010, 20:45
  4. Calling COM object from QT
    By mrityunjay in forum Qt Programming
    Replies: 0
    Last Post: 27th October 2009, 05:25
  5. Calling XPCOM Object from QT
    By sujith in forum Qt Programming
    Replies: 0
    Last Post: 3rd July 2009, 10:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.