PDA

View Full Version : how to copy QListWidget item into another dialog(form)



dwiandra
24th May 2017, 03:37
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)

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <secdialog.h>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_pushButton_clicked();

private:
Ui::MainWindow *ui;
SecDialog *secDialog;

};

#endif // MAINWINDOW_H


secDialog (2nd form)

#ifndef SECDIALOG_H
#define SECDIALOG_H

#include <QDialog>
#include <display.h>


namespace Ui {
class SecDialog;
}

class SecDialog : public QDialog
{
Q_OBJECT

public:
explicit SecDialog(QWidget *parent = 0);
~SecDialog();

private slots:
void on_pushButton_pkt1_clicked();

void on_pushButton_pkt2_clicked();

void on_pushButton_pkt3_clicked();

void on_pushButton_pkt4_clicked();

void on_pushButton_fanta_clicked();

void on_pushButton_cola_clicked();

void on_pushButton_air_clicked();

void on_pushButton_green_clicked();

void on_pushButton_display_clicked();

void on_pushButton_OK_clicked();

private:
Ui::SecDialog *ui;
display *Display;

};

#endif // SECDIALOG_H


Display (3rd form)


#ifndef DISPLAY_H
#define DISPLAY_H

#include <QDialog>

namespace Ui {
class display;
}

class display : public QDialog
{
Q_OBJECT

public:
explicit display(QWidget *parent = 0);
~display();

private:
Ui::display *ui;
};

#endif // DISPLAY_H


SOURCES SECTION

main.cpp


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QPixmap>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//cara menampilkan gambar
QPixmap pix(":/resource/img/magna-network-pt_fb.png"); //<-pake resouce
int w = ui->label_pic->width();
int h = ui->label_pic->height();
ui->label_pic->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));
//cara masukin iwdget ke status bar
//ui->statusBar->addPermanentWidget(ui->label);
//ui->statusBar->addPermanentWidget(ui->progressBar);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
//verifikasi login hard coded
QString username = ui->lineEdit_user->text();
QString password = ui->lineEdit_2_pass->text(); //qline edit trus echo mode ganti password biar tulisan ga keliatan

if(username == "admin" && password == "admin")
{
//untuk menampilkan message box
//QMessageBox::information(this,"Login","Success!");
hide();
//memanggil form lain
secDialog = new SecDialog(this);
secDialog->show();

//untuk menampilkan tulisan di bawah form
//argumen 1 buat textnya ,argumen 2 untuk berapa lama keluar textnya , 1000 = sedetik
//ui->statusBar->showMessage("Success!",2000);


}
else
{
//untuk menampilkan message box
//QMessageBox::warning (this,"Login","Failed login!");

//untuk menampilkan tulisan di bawah form
ui->statusBar->showMessage("Failed login!",2000);
}

}


secDialog.cpp


#include "secdialog.h"
#include "ui_secdialog.h"
#include <sstream>
#include <display.h>

SecDialog::SecDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SecDialog)
{
ui->setupUi(this);
}

SecDialog::~SecDialog()
{
delete ui;
}

void SecDialog::on_pushButton_pkt1_clicked()
{
QString paket1 = "Paket 1";
ui->listWidget->addItem(paket1);
}

void SecDialog::on_pushButton_pkt2_clicked()
{
QString paket2 = "Paket 2";
ui->listWidget->addItem(paket2);
}


void SecDialog::on_pushButton_pkt3_clicked()
{
QString paket3 = "Paket 3";
ui->listWidget->addItem(paket3);
}

void SecDialog::on_pushButton_pkt4_clicked()
{
QString paket4 = "Paket 4";
ui->listWidget->addItem(paket4);
}

void SecDialog::on_pushButton_fanta_clicked()
{
QString fanta = "Fanta";
ui->listWidget->addItem(fanta);
}

void SecDialog::on_pushButton_cola_clicked()
{
QString cola = "Cola";
ui->listWidget->addItem(cola);
}

void SecDialog::on_pushButton_air_clicked()
{
QString air = "Air Putih";
ui->listWidget->addItem(air);
}


void SecDialog::on_pushButton_green_clicked()
{
QString green = "Green Tea";
ui->listWidget->addItem(green);
}

void SecDialog::on_pushButton_display_clicked()
{
Display = new display (this);
Display -> show();
}

void SecDialog::on_pushButton_OK_clicked()
{

}


display.cpp


#include "display.h"
#include "ui_display.h"

display::display(QWidget *parent) :
QDialog(parent),
ui(new Ui::display)
{
ui->setupUi(this);
}

display::~display()
{
delete ui;
}


thanks for the attention

d_stranz
24th May 2017, 17:12
Make a custom signal for SecDialog, and add a member variable to keep track of what the user has ordered, something like:



class SecDialog : public QDialog
{
// ... constructor, destructor, etc.

signals:
void updateMenuItems( const QStringList & items );

// ...
private:
QStringList menuItems;
}


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:



void SecDialog::on_pushButton_pkt1_clicked()
{
QString paket1 = "Paket 1";
menuItems.push_bask( paket1 );
ui->listWidget->addItem(paket1);
}

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



void SecDialog::on_pushButton_OK_clicked()
{
emit updateMenuItems( menuItems );
}

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



public slots:
void onUpdateMenuItems( const QStringList & items );




void display::onUpdateMenuItems( const QStringList & items )
{
ui->listwidget->clear();
ui->listwidget->addItems( items );
}


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.

dwiandra
30th May 2017, 09:58
Make a custom signal for SecDialog, and add a member variable to keep track of what the user has ordered, something like:



class SecDialog : public QDialog
{
// ... constructor, destructor, etc.

signals:
void updateMenuItems( const QStringList & items );

// ...
private:
QStringList menuItems;
}


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:



void SecDialog::on_pushButton_pkt1_clicked()
{
QString paket1 = "Paket 1";
menuItems.push_bask( paket1 );
ui->listWidget->addItem(paket1);
}

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



void SecDialog::on_pushButton_OK_clicked()
{
emit updateMenuItems( menuItems );
}

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



public slots:
void onUpdateMenuItems( const QStringList & items );




void display::onUpdateMenuItems( const QStringList & items )
{
ui->listwidget->clear();
ui->listwidget->addItems( items );
}


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

d_stranz
31st May 2017, 00:59
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.