PDA

View Full Version : Cannot create a QAbstractItemModel::createIndex(r,c),



tonnot
20th April 2011, 10:51
I have a function to retrieve data from my model.
I want to call it using row,col (r,c) set by me.

return a_model->data(QAbstractItemModel::createIndex(r,c),Qt::Disp layRole);
Ok, I get
error: cannot call member function 'QModelIndex QAbstractItemModel::createIndex(int, int, void*) const' without object

Any idea ?

agarny
20th April 2011, 10:57
Yes, re-read the error message and you might then realise that you cannot just call QAbstractItemModel::createIndex. You need an object of type QAbstractItemModel to call that method...

wysota
20th April 2011, 10:59
He won't call it because it's a protected method :) But that's a completely different issue...

tonnot
20th April 2011, 11:07
Maybe I cannot call model::data ????

agarny
20th April 2011, 11:09
Maybe I cannot call model::data ????As Wysota said, it's a protected method, so...

wysota
20th April 2011, 11:21
As Wysota said, it's a protected method, so...

But this is irrelevant in this case, the compiler complains about something else. Only when he fixes the current problem the compiler will complain it's a protected method. Which then will lead to a real question - why is this method protected and should it be used in this context. So let's go one step at a time and let tonnot learn by his own mistakes (again).

@tonnot: I would really recommend you take some cash, go to a bookstore, buy some decent book on C++ and read it WITH UNDERSTANDING.

agarny
20th April 2011, 11:25
But this is irrelevant in this case, the compiler complains about something else. Only when he fixes the current problem the compiler will complain it's a protected method. Which then will lead to a real question - why is this method protected and should it be used in this context. So let's go one step at a time and let tonnot learn by his own mistakes (again).You are definitely more patient than I am! :)

wysota
20th April 2011, 11:36
You are definitely more patient than I am! :)

Not really. I just don't want to go through the same issue in two days when tonnot decides to use a regular method as a static one again. And since we had an argument when I called him an immature programmer, I'm treating him as a matrure programmer now assuming he does know what he is doing and that he indeed does know C++. Maybe finally he will decide he needs help improving his C++ skills.

agarny
20th April 2011, 11:38
Not really. I just don't want to go through the same issue in two days when tonnot decides to use a regular method as a static one again.Let me rephrase then: you are definitely more optimistic than I am! :)

wysota
20th April 2011, 11:39
Or more sarcastic ;)

tonnot
20th April 2011, 11:44
Thanks everybody and excuse me for my poor english and my poor c++ knowlegde.
I understand your explanations

And I'm glad to make you more fun with the task of addressing the forum

wysota
20th April 2011, 11:48
Just spend an hour a day for two months learning C++ (and I mean learning and not doing tutorials or copying code) and you will stop giving us reasons to answer the way we do.

changedsoul
28th June 2015, 20:10
Hello, I know this is an old thread, but I came across this same error message and I will lay this right out up front....I am an inexperienced programmer!! :)
I know help was not given to the OP because the claim was they had experience and this error suggested otherwise, but if someone could help me(an inexperienced programmer) out, I would greatly appreciate it.

I learn best by trying and failing. Well I tried, and failed, and got this error message. And then, created an object and got the next error, "its protected!!". So Can someone help explain why some times you can use class::method (QString::Number()) and it works, and some times QTreeWidget::itemFromIndex(), you cant?

I have read many C++ books, but they all seem to do the same thing. They start out teaching basics, loops, conditional statements, etc, then touch on classes, and then BAM, your in over your head. This is how you build a car....you take some nuts and bolts, arrange them in a certain order, and BAM you have a car....and your left scratching your head, well how come that nut and bolt went here and not there, and what happened to everything else. (poor example, but I hope it makes sense).

Thanks.

agarny
28th June 2015, 21:42
Seriously?! Read the first few paragraphs of:

http://www.cplusplus.com/doc/tutorial/classes/

changedsoul
29th June 2015, 04:06
well I appreciate the link. As I am still learning, it seems my problem is that of a design issue. Proper ways to design classes. Perhaps this comes from many years of experience programming and gaining understanding of just how everything works together. I am not there yet.

wysota
29th June 2015, 13:11
The problem is not a design issue but rather lack of knowledge regarding how the hammer (i.e. c++) works.

agarny
29th June 2015, 13:24
With all due respect to changedsoul, I think he first and foremost needs to learn how to read and understand what he reads. I mean, I cannot believe that the many (!?) C++ books he read don't mention anything about class access specifiers.

changedsoul
30th June 2015, 04:05
Its not that what I read does not mention or teach about classes and access specifiers, its that they way its presented is never in a way that makes any sense to me. As an example, I am a cnc machinist presently. To explain my point about the books teaching c++, would be like me saying "Here is a machine, it can do this and this and this, and has these buttons", but that doesn't tell you how to actually develop anything on it. It takes time and learning how to properly use the tool. So a c++ book can tell me what a class can contain, but I find little info "for my understanding" how to actually put it into practice, how it all fits together and why. And reading a book front to back does nothing for me because if I do not use it repeatidly, I most likely will forget it....

jefftee
30th June 2015, 06:48
Here's the simplified rules for using the public, protected, and private access modifiers:



Public variables or methods are freely available from both inside and outside of your class
Protected variables or methods are available to your class or any class that subclasses your class
Private variables or methods are only available to your class implementation


So generally, people make all of the class variables private, as well as methods that are not intended to be used by people using your class. Public should be used for methods you design that are intended to provide the interface for your class (how programmers interact with your class). Protected is a more advanced topic that you would implement for your class if it's intended that developers subclass your base class, etc.

There's more to it than what I've written here, but this should get you started.

wysota
30th June 2015, 08:32
Its not that what I read does not mention or teach about classes and access specifiers, its that they way its presented is never in a way that makes any sense to me. As an example, I am a cnc machinist presently. To explain my point about the books teaching c++, would be like me saying "Here is a machine, it can do this and this and this, and has these buttons", but that doesn't tell you how to actually develop anything on it. It takes time and learning how to properly use the tool. So a c++ book can tell me what a class can contain, but I find little info "for my understanding" how to actually put it into practice, how it all fits together and why. And reading a book front to back does nothing for me because if I do not use it repeatidly, I most likely will forget it....

Keeping to your example, what you are trying to do is that having seen a picture of a machine in a store window you start building an airplane and you are frustrated there are no tutorials how to build a plane without posessing any engineering skills. Considering that millions of people managed to grasp how C++ works, it is possible to achieve. You just have to do it before trying to do something complex with it.

You cannot expect every C++ feature you encounter to be explained to you by jefftee. I assure you he will eventually lose patience as well.

changedsoul
1st July 2015, 01:19
You cannot expect every C++ feature you encounter to be explained to you by jefftee. I assure you he will eventually lose patience as well.
Not trying to sound like I have an attitude, but I was not asking jefftee to explain everything encountered to me.
Thank you jefftee for taking the time to explain in a way that "does not" belittle the person asking the question and not posting responses that shows your superiority over the person asking and not making the poster feel like manure. I really do appreciate it.