[Solved] Subclassing QAbstractTableModel
-
Hi,
If the role is not something you handle, return the base class function's implementation otherwise your view won't have any information to show you anything
-
What do you mean with "handling the rol"? What else do I need to do?
-
@ if (role == Qt::DisplayRole)@
You only handle DisplayRole, returning QVariant::Invalid for every other role just gives the views nothing to work on.
What to do ? What I already wrote, for every other role return the base class implementation result of data and headerData
-
First, you CAN'T to return the base class implementation, because it's PURE virtual. You know, the data function is = 0.
And second, even no handling the rol (without condition) it shows nothing. -
Yes you can. You can have have a pure virtual function with a default implementation which is not the case for QAbstractTableModel.
Anyway, you are missing some checks like e.g. the validity for the index given as a parameter. Did you also ensure that all your methods are called ?
You can have a look at the Pixelator example, it uses a custom QAbstractTableModel
-
Let's see if we are talking about the same. If we have:
@#include <iostream>
using namespace std;class A
{
public:
virtual void foo() = 0; // Now, you NEED to override this method!
};void A::foo()
{
cout << "A::foo" << endl;
}class B : public A
{
public:
void foo() override; // Overriding base foo. You get a compiling error if not!:
// Cannot declare variable 'b' to be of abstract type 'B' B b; because the following virtual functions are pure within 'B'
};void B::foo()
{
cout << "B::foo" << endl;
}int main(int argc, char *argv[])
{
B b;b.foo(); return 0;
}@
As I said before, I can't to call a pure virtual function (=0). You need to override it on its derived class.
-
I stumbled upon a similar problem when reading the "Model/View Tutorial":http://qt-project.org/doc/qt-5/modelview.html . One of your problem(s) might be, that you create a Stack-variable "model" in your MainWindow constructor, which you then pass to the tableView as model. When the constructor is done, the Stack-variable won't exist anymore.
You could try allocating "model" on the heap and see if this is fixing your problem. I.e. change
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);ThreadModel model; ui->tableView->setModel(&model);
}@
... to ...
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);ThreadModel *model = new ThreadModel(); ui->tableView->setModel(model);
}@
-
yetanotherbender has the right solution, I missed that one in your code.
As for our little understanding problem:
@
#include <iostream>
using namespace std;class A
{
public:
virtual void foo() = 0; // Now, you NEED to override this method! << Correct and agreed
};void A::foo()
{
cout << "A::foo" << endl;
}class B : public A
{
public:
void foo() override; // Overriding base foo. You get a compiling error if not!: << still correct and agreed
};void B::foo()
{
A::foo() // If your B class doesn't do anything special you can call the base class default implementation
}int main(int argc, char *argv[])
{
B b;b.foo(); return 0;
}
@ -
[quote author="yetanotherbender" date="1403608883"]You could try allocating “model” on the heap and see if this is fixing your problem[/quote]
Great! That fixed the problem! Thank you SO F*much!! :D
SGaist, thank you for trying and being pacient!! :)
-
You're welcome !
Happy Qt coding ! :)