How to create a qtablewidget populated from a C++ object array?

2.6k views Asked by At

I've looked at many tutorials and examples, and I can't seem to find a decent example of a qtablewidget utilizing objects for the data. Nearly all detailed tutorials (other than hard-coded data) involve databases and qtableview.

Some Background: I've programmed a console program that calculates and displays the grades and assignments of students in a class. The teacher enters data like section weights, assignment names, attendance dates, assignment max scores, etc. She then enters the scores of these various assignments or attendance for each student. The teacher data is saved in a Teacher object (e.g. Teacher teacher; ), and the Student data is saved in an array of Student objects which makes up the class roster. (e.g. Student class_roster[50]; ) Obviously, various functions handle the calucations in their respective classes. It works perfectly...in a console.

Now, as kind of a monkey wrench, I have to make this a GUI program rather than console. Being C++, Qt seemed to be an interesting method that resembles Visual C#. After 3 days of trying to make this work, as my deadline fast appraoches, I'm succumbing to the fact that I don't know what I'm doing when it comes to Qt. Help would be most appreciated.

I have posted a picture of the GUI I designed in the Qt Creator. From that, can somebody provide me with a solid example of what I'm trying to accomplish? I hesitate requesting a good tutorial (because of the policy about that)

So, in a nutshell, what I need to know is:

1) How to Load the data from the text fields into my classes/objects.

2) How to populate the qtablewidgets with the data from the classes/objects.

Note: This is for a Testing class (V&V), so Qt is not the project. The code I've completed is what is important, so nothing I'm requesting here breaches any ethics. We just require a GUI for the harness.

GradeBook GUI Screenshot

1

There are 1 answers

1
Sommerwild On BEST ANSWER

Check the signals of the QTableWidget here http://doc.qt.io/qt-4.8/qtablewidget.html#details For example when a cell of your table is changed this signal is emited: void QTableWidget::itemChanged(QTableWidgetItem * item); Connect that signal to a slot where you send the data of the cell to your object, ie:

void pseudoSlot(QTableWidgetItem * item)
{ 
   if(item.column() == 1) //its last name
   {
       passLastNameDataToYourObjects(item.row, item->text()); //item->row() will be the index of the student
   }
   //continue with the rest of the cases
}

That covers the first question.

For populating your table, when you create it you just create the cells, fill them with your data and insert them, ie:

void pseudoFillTable(QTableWidget *table_)
{
    foreach(Student s, classRoster)
    {
        insertRow(table_.rowCount());
        QTableWidgetItem *lastNameItem = new QTableWidgetItem(s.getLastNameOrSomething());
        table_->setItem(table_.rowCount(), 1, lastNameItem); //the 1 is the index of the lastName column
        //and so on
    }
}

The code is an approximation but it maybe helps you to get the idea.