Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] qListWidget cannot add an item dynamically
Forum Updated to NodeBB v4.3 + New Features

[Solved] qListWidget cannot add an item dynamically

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LeeMinh
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      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();@

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LeeMinh
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved