0

How can I add a QSting and an int to a tableView at same time when a button is clicked?

What I want is to have the first column with names and the second column with numbers. Both items need to be added at the same row when a button is clicked.

    Col 1---      Col 2--- 

    First Name    1 
    Second Name   2 
    Third Name    3

Here is what I have which adds two strings, how can I convert the second cell to be an int?

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

    model = new QStandardItemModel();
    model->setRowCount(0);
    ui->tableView->setModel(model);
}

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

void MainWindow::on_pushButton_clicked()
{
    QStandardItem *userName = new QStandardItem(ui->lineEdit_Name->text());
    QStandardItem *userNumber = new QStandardItem(ui->lineEdit_Number->text());

    QList<QStandardItem*> row;
    row <<userName << userNumber;

    model->appendRow(row);
}

Thanks a lot

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
fs_tigre
  • 10,650
  • 13
  • 73
  • 146

2 Answers2

2

Does it really have to be an integer or can it just look like an integer?

Conventionally in tables, text is displayed left-aligned and numbers are displayed right-aligned. You can get this effect by setting the alignment for your second column:

QStandardItem *userNumber = new QStandardItem(ui->lineEdit_Number->text());
userNumber->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

If you need to use the data as an integer somewhere else in your program, you can convert the QString to an integer when you need it.

RobbieE
  • 4,280
  • 3
  • 22
  • 36
  • +1 for the algnment solution. To the record. The data in a QStandardItem is a QVariant. (for any role, check `QVariant QStandardItem::data(int role)` It's the default delegate who converts the displayRole to a QString. (and backgroundRole to a color .. etc etc etc) – trompa Aug 02 '13 at 13:16
  • The only reason I want to display them as an integers is because if one of the rows in the tableView is deleted a number outside the table will be updated to reflect those changes. In other words the ints will need to be added together once in the tableView. Do I need a custom Model and Delegate for this? – fs_tigre Aug 02 '13 at 13:50
  • You have to show that second colum number? If not, add it as a diferente Role (Qt::UserRole + 1) to the QStandardItem of the first column. Maybe you shoudl tell us a bit more about what you want to achive. So we could help you better – trompa Aug 02 '13 at 13:58
  • Here is my previous post, it kind of explains what I want to achieve. Let me add that I'm new to the MVC model or MV in Qt, I have been reading about it for the last two days but there is a lot of informaiton that needs to be learnt, getting there. http://stackoverflow.com/questions/17960194/how-to-display-a-selected-date-and-a-number-in-qt – fs_tigre Aug 02 '13 at 14:01
1

Ok, i will gather all in this answer:

First:

every data in a QStandardItemModel is a QVariant, so you can query the data either

  1. to the model with

    virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const

  2. to the QStandardItem with:

    virtual QVariant data ( int role = Qt::UserRole + 1 ) const

Both will return a QVariant. You can get the int with

int toInt(bool * ok = 0) const

Note that the bool is optional, but will return true if the conversion was posible.

Also, you can check if it can be converted to an int:

bool QVariant::canConvert(int targetTypeId) const

yourVariant.canConvert( QMetaType::Int ) should return true.

To get what you want. I would use takeRow method from QStandardItemModel:

 QList<QStandardItem *> takeRow(int row)

That its, the reverse to what you doing to append the row. So you know that you can ask the second element of the returned QList for the Int value.

trompa
  • 1,967
  • 1
  • 18
  • 26
  • Sorry but I'm a little confused, the documentation for takeRow() says: "Removes the item at (row, column) without deleting it" Why would I want to delete it? Sorry if I don't see the obvious. – fs_tigre Aug 02 '13 at 14:48
  • Removes the given row without deleting the row items, and returns a list of pointers to the removed items. http://qt-project.org/doc/qt-5.0/qtgui/qstandarditemmodel.html#takeRow - It removes it from the model. But doesnt call delete – trompa Aug 02 '13 at 14:51
  • ie. the objets still exist in memory, you can access em throutgh the returned list. But not from the model – trompa Aug 02 '13 at 14:56
  • I'm a little confused but I will give it a try. Your comments about how to query data help me to clarify some other doubts I had, Thanks a lot. – fs_tigre Aug 02 '13 at 15:40