Results 1 to 4 of 4

Thread: how to copy QListWidget item into another dialog(form)

  1. #1
    Join Date
    May 2017
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X

    Default how to copy QListWidget item into another dialog(form)

    So currently i'm building a project that would be like an order menu for a fast food restaurant, so what i did do is making 3 forms, first for the login, second is for ordering the food, and the third is just for display what food have been ordered,its kinda like kitchen display for a chef.

    my question
    1.how to copy the list from ListWidget in second form into ListView in 3rd form with a push button?
    2.is it possible to make a kind of timer that for like every 1 second that my ListView on 3rd form is refreshing? so it kinda make it always check if there's any new input from List Widget?

    if you can help it would be great, im making this for my school project, so any help would be amazing

    my code:
    HEADER SECTION
    main window (1st form)
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <secdialog.h>
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private slots:
    19. void on_pushButton_clicked();
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. SecDialog *secDialog;
    24.  
    25. };
    26.  
    27. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    secDialog (2nd form)
    Qt Code:
    1. #ifndef SECDIALOG_H
    2. #define SECDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <display.h>
    6.  
    7.  
    8. namespace Ui {
    9. class SecDialog;
    10. }
    11.  
    12. class SecDialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit SecDialog(QWidget *parent = 0);
    18. ~SecDialog();
    19.  
    20. private slots:
    21. void on_pushButton_pkt1_clicked();
    22.  
    23. void on_pushButton_pkt2_clicked();
    24.  
    25. void on_pushButton_pkt3_clicked();
    26.  
    27. void on_pushButton_pkt4_clicked();
    28.  
    29. void on_pushButton_fanta_clicked();
    30.  
    31. void on_pushButton_cola_clicked();
    32.  
    33. void on_pushButton_air_clicked();
    34.  
    35. void on_pushButton_green_clicked();
    36.  
    37. void on_pushButton_display_clicked();
    38.  
    39. void on_pushButton_OK_clicked();
    40.  
    41. private:
    42. Ui::SecDialog *ui;
    43. display *Display;
    44.  
    45. };
    46.  
    47. #endif // SECDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    Display (3rd form)

    Qt Code:
    1. #ifndef DISPLAY_H
    2. #define DISPLAY_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class display;
    8. }
    9.  
    10. class display : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit display(QWidget *parent = 0);
    16. ~display();
    17.  
    18. private:
    19. Ui::display *ui;
    20. };
    21.  
    22. #endif // DISPLAY_H
    To copy to clipboard, switch view to plain text mode 

    SOURCES SECTION

    main.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    4. #include <QPixmap>
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. //cara menampilkan gambar
    11. QPixmap pix(":/resource/img/magna-network-pt_fb.png"); //<-pake resouce
    12. int w = ui->label_pic->width();
    13. int h = ui->label_pic->height();
    14. ui->label_pic->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));
    15. //cara masukin iwdget ke status bar
    16. //ui->statusBar->addPermanentWidget(ui->label);
    17. //ui->statusBar->addPermanentWidget(ui->progressBar);
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void MainWindow::on_pushButton_clicked()
    26. {
    27. //verifikasi login hard coded
    28. QString username = ui->lineEdit_user->text();
    29. QString password = ui->lineEdit_2_pass->text(); //qline edit trus echo mode ganti password biar tulisan ga keliatan
    30.  
    31. if(username == "admin" && password == "admin")
    32. {
    33. //untuk menampilkan message box
    34. //QMessageBox::information(this,"Login","Success!");
    35. hide();
    36. //memanggil form lain
    37. secDialog = new SecDialog(this);
    38. secDialog->show();
    39.  
    40. //untuk menampilkan tulisan di bawah form
    41. //argumen 1 buat textnya ,argumen 2 untuk berapa lama keluar textnya , 1000 = sedetik
    42. //ui->statusBar->showMessage("Success!",2000);
    43.  
    44.  
    45. }
    46. else
    47. {
    48. //untuk menampilkan message box
    49. //QMessageBox::warning (this,"Login","Failed login!");
    50.  
    51. //untuk menampilkan tulisan di bawah form
    52. ui->statusBar->showMessage("Failed login!",2000);
    53. }
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 

    secDialog.cpp

    Qt Code:
    1. #include "secdialog.h"
    2. #include "ui_secdialog.h"
    3. #include <sstream>
    4. #include <display.h>
    5.  
    6. SecDialog::SecDialog(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::SecDialog)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. SecDialog::~SecDialog()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void SecDialog::on_pushButton_pkt1_clicked()
    19. {
    20. QString paket1 = "Paket 1";
    21. ui->listWidget->addItem(paket1);
    22. }
    23.  
    24. void SecDialog::on_pushButton_pkt2_clicked()
    25. {
    26. QString paket2 = "Paket 2";
    27. ui->listWidget->addItem(paket2);
    28. }
    29.  
    30.  
    31. void SecDialog::on_pushButton_pkt3_clicked()
    32. {
    33. QString paket3 = "Paket 3";
    34. ui->listWidget->addItem(paket3);
    35. }
    36.  
    37. void SecDialog::on_pushButton_pkt4_clicked()
    38. {
    39. QString paket4 = "Paket 4";
    40. ui->listWidget->addItem(paket4);
    41. }
    42.  
    43. void SecDialog::on_pushButton_fanta_clicked()
    44. {
    45. QString fanta = "Fanta";
    46. ui->listWidget->addItem(fanta);
    47. }
    48.  
    49. void SecDialog::on_pushButton_cola_clicked()
    50. {
    51. QString cola = "Cola";
    52. ui->listWidget->addItem(cola);
    53. }
    54.  
    55. void SecDialog::on_pushButton_air_clicked()
    56. {
    57. QString air = "Air Putih";
    58. ui->listWidget->addItem(air);
    59. }
    60.  
    61.  
    62. void SecDialog::on_pushButton_green_clicked()
    63. {
    64. QString green = "Green Tea";
    65. ui->listWidget->addItem(green);
    66. }
    67.  
    68. void SecDialog::on_pushButton_display_clicked()
    69. {
    70. Display = new display (this);
    71. Display -> show();
    72. }
    73.  
    74. void SecDialog::on_pushButton_OK_clicked()
    75. {
    76.  
    77. }
    To copy to clipboard, switch view to plain text mode 

    display.cpp

    Qt Code:
    1. #include "display.h"
    2. #include "ui_display.h"
    3.  
    4. display::display(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::display)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. display::~display()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

    thanks for the attention

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to copy QListWidget item into another dialog(form)

    Make a custom signal for SecDialog, and add a member variable to keep track of what the user has ordered, something like:

    Qt Code:
    1. class SecDialog : public QDialog
    2. {
    3. // ... constructor, destructor, etc.
    4.  
    5. signals:
    6. void updateMenuItems( const QStringList & items );
    7.  
    8. // ...
    9. private:
    10. QStringList menuItems;
    11. }
    To copy to clipboard, switch view to plain text mode 

    The "menuItems" variable is to make it easier to retrieve the list of items the user has ordered. Otherwise, you would have to go into the list widget, retrieve each QListWidgetItem and then get the text for the item.

    In each of your slots where the user has clicked a button to place an order, first push the item onto the string list, then add it to the widget:

    Qt Code:
    1. void SecDialog::on_pushButton_pkt1_clicked()
    2. {
    3. QString paket1 = "Paket 1";
    4. menuItems.push_bask( paket1 );
    5. ui->listWidget->addItem(paket1);
    6. }
    To copy to clipboard, switch view to plain text mode 

    When the user clicks the "OK" button, you will emit the updateMenuItems() signal and send the list of selected items:

    Qt Code:
    1. void SecDialog::on_pushButton_OK_clicked()
    2. {
    3. emit updateMenuItems( menuItems );
    4. }
    To copy to clipboard, switch view to plain text mode 

    In the "display" dialog class, add a public slot to receive this signal:

    Qt Code:
    1. public slots:
    2. void onUpdateMenuItems( const QStringList & items );
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void display::onUpdateMenuItems( const QStringList & items )
    2. {
    3. ui->listwidget->clear();
    4. ui->listwidget->addItems( items );
    5. }
    To copy to clipboard, switch view to plain text mode 

    In your SecDialog class, when you create the display dialog instance, you connect the signal from SecDialog to the slot in display.

    Note that your code contains at least one bug: you should not create the display class in the on_pushbutton_display_clicked() slot. This code will result in a new display instance being created each time the button is clicked. You probably want only one of these, so create it in the SecDialog constructor and connect the signal and slot there.

    There are several more things you have to think about: When customer1 has placed the order, you need to have some way of clearing the list of items selected (and the list widget) so customer2 can start a new order. You also need to allow the customers to change their minds - if they order "Paket 1" but decide they don't really want it, how can they remove it from their order?

    And when the chef finishes with the order, how can he erase the list of items? And how does the chef know which item goes to which customer?

    I don't think a timer is a good idea. If the customer is entering their order then they change their mind and decide to remove some item, the timer might have already caused the item to be sent to the chef's list. Thus, the chef thinks he has to prepare something that the customer does not want. It is better to have a positive "commit" action (like clicking the OK button) that sends the final order to the chef.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2017
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: how to copy QListWidget item into another dialog(form)

    Quote Originally Posted by d_stranz View Post
    Make a custom signal for SecDialog, and add a member variable to keep track of what the user has ordered, something like:

    Qt Code:
    1. class SecDialog : public QDialog
    2. {
    3. // ... constructor, destructor, etc.
    4.  
    5. signals:
    6. void updateMenuItems( const QStringList & items );
    7.  
    8. // ...
    9. private:
    10. QStringList menuItems;
    11. }
    To copy to clipboard, switch view to plain text mode 

    The "menuItems" variable is to make it easier to retrieve the list of items the user has ordered. Otherwise, you would have to go into the list widget, retrieve each QListWidgetItem and then get the text for the item.

    In each of your slots where the user has clicked a button to place an order, first push the item onto the string list, then add it to the widget:

    Qt Code:
    1. void SecDialog::on_pushButton_pkt1_clicked()
    2. {
    3. QString paket1 = "Paket 1";
    4. menuItems.push_bask( paket1 );
    5. ui->listWidget->addItem(paket1);
    6. }
    To copy to clipboard, switch view to plain text mode 

    When the user clicks the "OK" button, you will emit the updateMenuItems() signal and send the list of selected items:

    Qt Code:
    1. void SecDialog::on_pushButton_OK_clicked()
    2. {
    3. emit updateMenuItems( menuItems );
    4. }
    To copy to clipboard, switch view to plain text mode 

    In the "display" dialog class, add a public slot to receive this signal:

    Qt Code:
    1. public slots:
    2. void onUpdateMenuItems( const QStringList & items );
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void display::onUpdateMenuItems( const QStringList & items )
    2. {
    3. ui->listwidget->clear();
    4. ui->listwidget->addItems( items );
    5. }
    To copy to clipboard, switch view to plain text mode 

    In your SecDialog class, when you create the display dialog instance, you connect the signal from SecDialog to the slot in display.

    Note that your code contains at least one bug: you should not create the display class in the on_pushbutton_display_clicked() slot. This code will result in a new display instance being created each time the button is clicked. You probably want only one of these, so create it in the SecDialog constructor and connect the signal and slot there.

    There are several more things you have to think about: When customer1 has placed the order, you need to have some way of clearing the list of items selected (and the list widget) so customer2 can start a new order. You also need to allow the customers to change their minds - if they order "Paket 1" but decide they don't really want it, how can they remove it from their order?

    And when the chef finishes with the order, how can he erase the list of items? And how does the chef know which item goes to which customer?

    I don't think a timer is a good idea. If the customer is entering their order then they change their mind and decide to remove some item, the timer might have already caused the item to be sent to the chef's list. Thus, the chef thinks he has to prepare something that the customer does not want. It is better to have a positive "commit" action (like clicking the OK button) that sends the final order to the chef.
    Thank you for the answer, but sadly for me it doesn't work, i follow your instruction above and when i compile it, it doesnt work,the item that i input on secDialog it didn't show on the display, or am i missing some code?

    Note that your code contains at least one bug: you should not create the display class in the on_pushbutton_display_clicked() slot. This code will result in a new display instance being created each time the button is clicked. You probably want only one of these, so create it in the SecDialog constructor and connect the signal and slot there.

    There are several more things you have to think about: When customer1 has placed the order, you need to have some way of clearing the list of items selected (and the list widget) so customer2 can start a new order. You also need to allow the customers to change their minds - if they order "Paket 1" but decide they don't really want it, how can they remove it from their order?

    And when the chef finishes with the order, how can he erase the list of items? And how does the chef know which item goes to which customer?
    yeah i was thinking is it possible if i can code for when i clicking on an item on the third dialog and when i click a pushButton and the item that i selected will disappear?

    well for my project it doesn't matter which food to which consumer, this is just for showing that i can input an item from second dialog into the third dialog continuously and delete the selected item on the third dialog.

    it seems complicated for me, but i will gratefully thankful if you would to help me again and im sorry if my english were bad, its not my first language
    Last edited by dwiandra; 30th May 2017 at 10:10.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to copy QListWidget item into another dialog(form)

    The code I posted wasn't meant to be compiled, it was to give you an idea of how to proceed. Since this is your homework project and not mine, I'm not going to write your code for you. Study what I wrote, understand it, and then try to write your own version of it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QListWidget, how to copy selected items to clipboard
    By mnrao1230 in forum Qt Programming
    Replies: 3
    Last Post: 24th February 2012, 10:54
  2. Replies: 2
    Last Post: 22nd November 2010, 21:49
  3. Data from dialog to form
    By matulik in forum Newbie
    Replies: 3
    Last Post: 12th May 2010, 13:36
  4. Replies: 1
    Last Post: 3rd February 2010, 07:44
  5. How to Switch the dialog Form
    By Smiler in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2009, 11:36

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.