[Solved] qListWidget cannot add an item dynamically



  • In my Qt application, I want to add a new item dynamically into a listview. Besides I also used Signal & Slot to transfer data between forms so I have created 2 following forms:

    mainwindow.h

    @
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    public slots:
    void ReceivedData(QString item);

    private slots:
    void on_btnAdd_clicked();

    void on_btnCancel_clicked();
    

    private:
    Ui::MainWindow *ui;

    void SetUpListName();
    

    };
    @

    addform.h

    @
    class AddForm : public QDialog
    {
    Q_OBJECT

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

    signals:
    void SendData(QString item);

    private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();
    

    private:
    Ui::AddForm *ui;
    MainWindow *main_window;
    };
    @

    mainwindow.cpp

    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    SetUpListName();
    }

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

    void MainWindow::SetUpListName()
    {
    // Add 5 new elements
    for (int i = 0; i < 5; i++) {
    QString item = "Item " + QString::number(i);
    ui->lwListItem->addItem(item);

    }
    

    }

    void MainWindow::on_btnAdd_clicked()
    {
    // Open Add Form
    AddForm add;
    add.setModal(true);
    add.exec();
    }

    void MainWindow::on_btnCancel_clicked()
    {
    this->close();
    }

    void MainWindow::ReceivedData(QString item)
    {
    // Check to receive data
    qDebug() << "Item: " << item;

    // Add a new item to list items
    ui->lwListItem->addItem(item);
    

    }
    @

    addform.cpp

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

    main_window = new MainWindow();
    connect(this, SIGNAL(SendData(QString)), main_window, SLOT(ReceivedData(QString)));
    

    }

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

    void AddForm::on_pushButton_clicked()
    {
    // Send data via Signal & Slot
    emit SendData(ui->txtName->text());
    }

    void AddForm::on_pushButton_2_clicked()
    {
    this->close();
    }
    @

    When I run the application, I got the data from Add form but the list view doesn't add this item.

    !http://i249.photobucket.com/albums/gg220/HienBoo/send_data_list_view.png(Cannot add an item dynamically)!

    Does someone have any solutions?

    Thanks!
    Lee

    P/S: You can download my source code at "here":http://www.mediafire.com/download/9djf6bazr8b18n3/Demo.rar.



  • Hi
    in this code you create new main window, it is wrong:
    addform.cpp
    @main_window = new MainWindow();
    connect(this, SIGNAL(SendData(QString)), main_window, SLOT(ReceivedData(QString)));@
    you need connect signal and slot here:
    mainwindow.cpp
    @AddForm add;
    add.setModal(true);
    connect(&add, SIGNAL(SendData(QString)), this, SLOT(ReceivedData(QString)));
    add.exec();@



  • @qxoz: I have fixed this issue. Thanks for your remainder!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.