Beginnerquestion: QStandardItemModel return and more



  • Hello guys! Im a beginner and i have a lot of questions. Im sorry for my broken english but i hope, you can help me.
    The problem is: "model" dont works. Have a look at "void Dialog::showData". So i cant use the "model" for my example. I hope you understand my "simple" problem. I think, i have to "return" the "model" at "void Dialog::setDataTable()" and i have to change the "void" -> (?) and what should i "return?

    What should the "script" to? The first one, it create a table (setDataTable). It should read two numbers from a combo-box (button-click) , check values of a table and show a message-box (showData).

    I really want to know, how you do that. Im really sorry for this "stupid question". :(

    Your's faithfully
    Rutschuru

    Heres the "beginner-code":

    @
    mainwindow.cpp

    void MainWindow::on_pushButton_clicked()
    {
    Dialog dialog;

    QString value1 = ui->comboBox->currentText();
    QString value2 = ui->comboBox_2->currentText();
    
    dialog.showData(value1, value2);
    

    }

    dialog.h

    public:
    void setDataTable();
    void showData(QString value1, QString value2);

    dialog.cpp:

    void Dialog::setDataTable()
    {
    QStandardItemModel *model = new QStandardItemModel.....
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("5")));
    (...)

    model->setVerticalHeaderItem(0, new QStandardItem(QString("5")));
    (...)
    
    QStandardItem *firstRow0 = new QStandardItem(QString("1"));
    QStandardItem *firstRow1 = new QStandardItem(QString("2"));
    (...)
    
    model->setItem(0,0,firstRow0);
    (...)
    
    ui->tableView->setModel(model);
    

    }

    void Dialog::showData(QString value1, QString value2)
    {

    QMessageBox::information(this, "", model->item(QString::number(iValue1),QString::number(iValue2))->text()
                            , "");
    

    }
    @



  • Oh, do nobody know a solution for this problem? :( Or did I have written incomprensible?
    I really want only to know, how I can return "model".

    Your’s faithfully
    Rutschuru


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Just to be sure I understand you right, do you want to do something like:

    Fill a model with data

    Ask the user for a row and a column

    Show the content of the cell corresponding to the row and column given by the user

    Is that right ?



  • Hi,
    In your showData function you give the model->item function QStrings as arguments, but it only accepts int row, int column. The QString::number will result in a QString. Does this software compile at all???
    Also, it is better to chop up that piece of code. First determine the row/column int values, check that they are within the model table, extract the QStandardItem * from the model the row/column point to (the model->item function). Check if that pointer isn't NULL, and only then read out the text.
    It sounds a bit comprehensive, but otherwise crashes of your App will occur.
    And like Gaist says, first fill your table with enough data in the constructor to be able to read our some stuff ;-)



  • Hello!

    thank you very much for your reply! Yes, thats correct at SGaist
    Im show you the "full code". Its works. But Its dont works because of the "model"-problem.

    Thats tricky, jeroentJe :S Do you have a "little example"?

    main.cpp
    @
    // owned header
    #include "mainwindow.h"
    // extern header
    #include <QApplication>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    mainwindow.cpp
    @
    // owned header
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"

    // extern header
    #include <QMessageBox>
    #include <QStringList>
    #include <QString>
    #include <QComboBox>

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

    // fill data to table
    fillData();
    
    // create table and put it in
    setDataTable();
    

    }

    void MainWindow::on_pushButton_clicked()
    {
    Dialog dialog;

    // read combobox
    QString value1 = ui->comboBox->currentText();
    QString value2 = ui->comboBox_2->currentText();
    
    // send data to dialog.showdata
    dialog.showData(value1, value2);
    

    }

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

    void MainWindow::setDataTable()
    {
    Dialog dialog;
    dialog.setDataTable();
    }

    void MainWindow::fillData()
    {
    QStringList box1;
    box1.append("5");
    box1.append("10");
    box1.append("15");
    box1.append("20");
    ui->comboBox->addItems(box1);

    QStringList box2;
    box2.append("5");
    box2.append("10");
    box2.append("15");
    box2.append("20");
    ui->comboBox_2->addItems(box2);
    

    }
    @

    dialog.cpp
    @
    // owned header
    #include "dialog.h"
    #include "ui_dialog.h"

    // extern header
    #include <QMessageBox>
    #include <QStandardItemModel>

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

    }

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

    // creating table
    void Dialog::setDataTable()
    {

    QStandardItemModel *model = new QStandardItemModel(2,3,this); //2 Rows and 3 Columns
    
    model->setHorizontalHeaderItem(0, new QStandardItem(QString("5")));
    model->setHorizontalHeaderItem(1, new QStandardItem(QString("10")));
    model->setHorizontalHeaderItem(2, new QStandardItem(QString("15")));
    model->setHorizontalHeaderItem(2, new QStandardItem(QString("20")));
    
    model->setVerticalHeaderItem(0, new QStandardItem(QString("5")));
    model->setVerticalHeaderItem(1, new QStandardItem(QString("10")));
    model->setVerticalHeaderItem(2, new QStandardItem(QString("15")));
    model->setVerticalHeaderItem(3, new QStandardItem(QString("20")));
    
    QStandardItem *firstRow0 = new QStandardItem(QString("1"));
    QStandardItem *firstRow1 = new QStandardItem(QString("2"));
    QStandardItem *firstRow2 = new QStandardItem(QString("3"));
    QStandardItem *firstRow3 = new QStandardItem(QString("4"));
    QStandardItem *firstRow4 = new QStandardItem(QString("5"));
    QStandardItem *firstRow5 = new QStandardItem(QString("6"));
    QStandardItem *firstRow6 = new QStandardItem(QString("7"));
    QStandardItem *firstRow7 = new QStandardItem(QString("8"));
    QStandardItem *firstRow8 = new QStandardItem(QString("9"));
    QStandardItem *firstRow9 = new QStandardItem(QString("10"));
    QStandardItem *firstRow10 = new QStandardItem(QString("11"));
    QStandardItem *firstRow11 = new QStandardItem(QString("12"));
    QStandardItem *firstRow12 = new QStandardItem(QString("13"));
    QStandardItem *firstRow13 = new QStandardItem(QString("14"));
    QStandardItem *firstRow14 = new QStandardItem(QString("15"));
    QStandardItem *firstRow15 = new QStandardItem(QString("16"));
    
    model->setItem(0,0,firstRow0);
    model->setItem(0,1,firstRow1);
    model->setItem(0,2,firstRow2);
    model->setItem(0,3,firstRow3);
    
    model->setItem(1,0,firstRow4);
    model->setItem(1,1,firstRow5);
    model->setItem(1,2,firstRow6);
    model->setItem(1,3,firstRow7);
    
    model->setItem(2,0,firstRow8);
    model->setItem(2,1,firstRow9);
    model->setItem(2,2,firstRow10);
    model->setItem(2,3,firstRow11);
    
    model->setItem(4,0,firstRow12);
    model->setItem(4,1,firstRow13);
    model->setItem(4,2,firstRow14);
    model->setItem(4,3,firstRow15);
    
    ui->tableView->setModel(model);
    

    }

    // show data
    void Dialog::showData(QString value1, QString value2)
    {

    // Test 1: Dont works
    //QMessageBox::information(this, "", model->item(value1,value2)->text()
      //                      , "");
    
    // Test 2 without "model":
    QMessageBox::information(this,"",value1 + value2, "");
    

    }
    @

    dialog.h
    @
    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    void setDataTable();
    void showData(QString value1, QString value2);

    private:
    Ui::Dialog *ui;
    };

    #endif // DIALOG_H

    @

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private slots:
    void fillData();
    void setDataTable();

    void on_pushButton_clicked();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

    @



  • Hi,
    You have a basic C++ error ;-)
    @
    void Dialog::setDataTable()
    {

    QStandardItemModel *model = new QStandardItemModel(2,3,this); //2 Rows and 3 Columns
    

    @
    This makes a model in the setDataTable function, but when that function is destroyed (ended) also the model pointer will get deleted. So in your next showData you want to use this pointer..........it isn't there and the compiler does not know who model pointer is.
    Two ways to fix it:
    1: Make the model pointer a class member and allocate data in the constructor
    2: Get the model pointer back from the View, in this case
    @
    ui->tableView->model()
    @
    That will return the model pointer to your data.
    Hope this helps



  • Hello Jeroen,

    thank you for your reply.

    Please, dont be angry. I know its Basics. :(

    So I want for my first try, to make a class:

    dialog.h
    @
    QAbstractItemModel model
    @

    But how can I exactly do that?

    Your’s faithfully
    Rutschuru


  • Lifetime Qt Champion

    Following your logic, why do you have the model in the dialog ? It should be part of the MainWindow, Dialog being only used to retrieve the row/column the user wants to see.



  • Hello SGaist! Sorry for my late reply.

    You say, its completly wrong to put it in a dialog? So thank you for this tip. Its because for my practice to learn much things (for example: using pointer, class, returns(!), parameters, etc.) "better". I hope, you understand. :)

    Im still trying. But is theres a "little example" how can I try it?


  • Lifetime Qt Champion

    Since you want to show something from the model based on a user input, why recreate the model each time you want to get the user input ?

    I would recommend that you take a look at Qt's documentation examples and demos (specially the MVC part) They are very well explained and should give you a good base to start


Log in to reply
 

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