Results 1 to 8 of 8

Thread: correct way to share data between 2 UI classes ?

  1. #1

    Default correct way to share data between 2 UI classes ?

    Hello,
    I have 2 forms created with QT Creator ( example MainWindow and an Importdialog ). Both have a treewidget and I would like to be able to copy treewidgetitems from the Import dialog to the mainwindow. How do I make the items I copy in the import dialog available to the MainWindow ( scope ? ) ?
    Thank you
    newbie

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: correct way to share data between 2 UI classes ?

    Do you know how to populate a QTreeWidget?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3

    Default Re: correct way to share data between 2 UI classes ?

    Hi,
    I hope so :-). The following is a SLOT in the Import dialog:

    Qt Code:
    1. if (ui->treeWidget->selectedItems().length()==1) {
    2. c_item=ui->treeWidget->selectedItems().at(0);
    3. }
    To copy to clipboard, switch view to plain text mode 
    The problem is that in this Ui class the MainWindow is not visible/not in scope, so I cannot simply do MainWindow::insertmywidget (c_item ).

    Thank you
    Last edited by high_flyer; 22nd August 2011 at 14:52. Reason: code tags

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: correct way to share data between 2 UI classes ?

    no, this code does not populate the tree.
    This code gets a selected item from the tree if only one item is selected.
    So this means you don't know how to populate a three.
    Start there.
    Learn how a tree is populated, this will help you then with the question you posted.
    Post again if you still need clarifications.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5

    Default Re: correct way to share data between 2 UI classes ?

    Must have misunderstood your question ...

    In the Mainwindow I add treewidgetitems like this for example:

    QTreeWidgetItem *qtr=ui->treeWidget->currentItem();
    if (qtr != NULL ){
    QTreeWidgetItem *cities = new QTreeWidgetItem(qtr);
    cities->setText(0, ui->lineEdit_2->text()); // Description
    QString b=ui->lineEdit_3->text(); // lat/long
    b.append("/");

    etc.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: correct way to share data between 2 UI classes ?

    The problem is that in this Ui class the MainWindow is not visible/not in scope, so I cannot simply do MainWindow::insertmywidget (c_item ).
    Why not?
    Visible does not mean it does not exist in memory.
    Your problem is not so clear.
    Please explain in more details exactly what you are trying to achieve this will help increase the quality of the help you'll receive.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7

    Default Re: correct way to share data between 2 UI classes ?

    Hi again,
    1. Created with QT creator an application with a mainwindow and another window which is created based on a action in the MainWindow ( Button "import data" ) -> works fine
    2. MainWindow w is instantiated in main.cpp ( default way how QTcreator does that )
    3. I click the "import data" button in the mainwindow, open file selector dialog, read my csv file, display the contents in a treewidget -> all OK
    3. then I select treewidgetitems in that treewidget , select "copy button" but when handling this signal in a slot in the import dialog class, the MainWindow seems not accessible.
    I can't call any method/slot in the Mainwindow class. I assume this is because Mainwindow w is not in scope since it was declared in main.cpp and not in importclass.cpp. How do I make
    it accessible ?

    Thank you

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: correct way to share data between 2 UI classes ?

    if both the main window and you copy dialog are in the main() scope, you can make the connection there.
    But it would be semantically more correct to make the copy dialog a child of your main window, this way you also haver direct access to both widgets.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. To Share a variable among classes
    By JenniferF in forum Newbie
    Replies: 2
    Last Post: 12th May 2011, 11:13
  2. Ways to share data between threads
    By AndresBarbaRoja in forum Qt Programming
    Replies: 8
    Last Post: 11th March 2011, 16:47
  3. [SOLVED] share data between caller and slot
    By whites11 in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2010, 13:34
  4. What is the best way to share data between 2 programs?
    By ModeZt in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2008, 21:23
  5. Help me to use QtSharedMemory to share the data objects
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 11:50

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.