Results 1 to 3 of 3

Thread: Problems with adding new window

  1. #1
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Problems with adding new window

    Hi, First off let me apologize if this doesn't make much sense at first.

    I might just be going about this all wrong, but I'm trying to have a second window open and use that for getting input. What I have works OK until I try to send the result back to the main window.

    gui is my main class(QMainWindow) and list(QWidget) is my second class that opens when list_button is clicked.
    Qt Code:
    1. void gui::on_list_btn_clicked()
    2. {
    3. list *mileage_list = new list;
    4. mileage_list->populate_list(mileage);
    5. mileage_list->show();
    6. }
    To copy to clipboard, switch view to plain text mode 
    populate_list() works fine also as expected.
    Qt Code:
    1. void list::populate_list(QVector<Entry> mileage)
    2. {
    3. QString list_item;
    4. QString format_date;
    5.  
    6. ui->listWidget->addItem("Date Miles Gallons");
    7.  
    8. for(int i = 0; i < mileage.size(); ++i)
    9. {
    10. for(int j = 0; j < mileage[i].date.length(); ++j)
    11. {
    12. format_date.append(mileage[i].date[j]);
    13. if(j == 1 || j == 3)
    14. {
    15. format_date.append("/");
    16. }
    17. }
    18. list_item = format_date % " "
    19. % QString::number(mileage[i].miles) % " "
    20. % QString::number(mileage[i].gallons);
    21. ui->listWidget->addItem(list_item);
    22. format_date = "";
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 
    I could just add the QListWidget to the main window, but I'd rather have it a separate window to save space in the main one. The way its supposed to work is to let the user select a date from the list and pass the index back to the main window which obviously doesn't work as I have it.
    Qt Code:
    1. void list::on_listWidget_itemClicked(QListWidgetItem *item)
    2. {
    3. int start_index = ui->listWidget->row(item);
    4.  
    5. //gui->set_index(start_index);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void gui::set_index(int index)
    2. {
    3. ui->start_date_txtBox->setText(mileage[index].date);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Besides the problem of sending the index back(I feel like I'm tripping over a crack in the sidewalk here), I don't like that the list needs to be closed separately and it should close with the main window if not done so first. Can someone suggest either a way to make this work as is or where I should look to get it working more efficiently. If I left something out(which I probably did), just let me know and I'll fill in the gaps.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems with adding new window

    For sending the index back to the main window I would suggest using a signal.

    In the header for list add a signals section to the class declaration
    Qt Code:
    1. signals:
    2. void indexSelected(int index);
    To copy to clipboard, switch view to plain text mode 
    or any other name you'd like.

    In on_listWidget_itemClicked() you emit the signal
    Qt Code:
    1. emit indexSelected(start_index);
    To copy to clipboard, switch view to plain text mode 

    Make gui::set_index a slot by putting it into a slots section of the class, e.g.
    Qt Code:
    1. private slots:
    2. void set_index(int index);
    To copy to clipboard, switch view to plain text mode 

    Then connect at the place where you create the secondary window
    Qt Code:
    1. connect(mileage_list, SIGNAL(indexSelected(int)), this, SLOT(set_index(int)));
    To copy to clipboard, switch view to plain text mode 

    As for closing the sub window when the main window closes: make it a QDialog subclass (instead of deriving from QWidget) and pass the main window as the dialog's parent

    Qt Code:
    1. list *mileage_list = new list(this);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    admkrk (27th April 2014)

  4. #3
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems with adding new window

    Thanks! I've been having a hard time getting a handle on signals and slots and you just made it a lot clearer for me since I've been relying on Designer to handle that for me up to now.

    I'll need to look into subclassing a bit more (I'm bad with terminology so it might just be a matter of, "oh you mean that" lol). I had already tried simply parenting it and that did not work. What I have works for now though and I'll worry more about that when I work on the cosmetics.

Similar Threads

  1. Replies: 3
    Last Post: 29th August 2011, 22:39
  2. Replies: 0
    Last Post: 6th May 2011, 12:02
  3. QMdiArea and adding sub window
    By sajis997 in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2011, 17:16
  4. QGraphicsLayout: Problems adding QLabel
    By SneakyPeterson in forum Newbie
    Replies: 1
    Last Post: 1st July 2010, 12:31
  5. Problems compiling after adding an action in a menu
    By grub87 in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 17:58

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.