PDA

View Full Version : Having trouble interpreting an error message. Is my C++ that bad?



johnny_sparx
1st April 2006, 19:44
I have a class where I defined the following:

const QModelIndex *Current_Model_Index;

The class has a slot that defines the value of Current_Model_Index (a slot):


void AcquisitionBrowserWidget::Display_File_Path(QStrin g *File_Path_Clicked, const QModelIndex *index)
{
Acquisition_Browser->MainFilePathDisplay->setText(*File_Path_Clicked);
// Update the internal target file path.
Target_File_Path = *File_Path_Clicked;
Current_Model_Index = index;
Update_All_Browser_Information();
//Acquisition_Browser->Transfer_Progress_Bar->setValue(0);
}

The following bit of code results in an error that I can't seem to interpret:

Current_Model_Index->model()->setData(*Current_Model_Index,Qt::blue,Qt::TextColo rRole);

The error:

roetgen 1121% make
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/export/data/jsalik/local/Qt-4.1.1/mkspecs/linux-g++ -I. -I/export/data/jsalik/local/Qt-4.1.1/include/QtCore -I/export/data/jsalik/local/Qt-4.1.1/include/QtGui -I/export/data/jsalik/local/Qt-4.1.1/include -I. -I. -I. -o AcquisitionBrowserWidget.o AcquisitionBrowserWidget.cpp
AcquisitionBrowserWidget.cpp: In member function `void
AcquisitionBrowserWidget::DICOM_Tree_Changed(QTree WidgetItem*, int)':
AcquisitionBrowserWidget.cpp:225: error: passing `const QAbstractItemModel' as
`this' argument of `virtual bool QAbstractItemModel::setData(const
QModelIndex&, const QVariant&, int)' discards qualifiers
AcquisitionBrowserWidget.cpp:202: warning: unused parameter `int column'
AcquisitionBrowserWidget.cpp: In member function `void
AcquisitionBrowserWidget::DICOM_Tree_Item_Double_C licked(QTreeWidgetItem*,
int)':
AcquisitionBrowserWidget.cpp:708: warning: unused parameter `int column'
AcquisitionBrowserWidget.cpp: In member function `int
AcquisitionBrowserWidget::Move_Files(QString, QString)':
AcquisitionBrowserWidget.cpp:799: warning: unused variable `
QClipboard*clipboard'
make: *** [AcquisitionBrowserWidget.o] Error 1
roetgen 1122%

The function where the error occurs is:

void AcquisitionBrowserWidget::DICOM_Tree_Changed(QTree WidgetItem * item, int column )
{
if(Target_File_Path!="" || Acquisition_Browser->Read_Only_Checkbox->checkState()==Qt::Unchecked)
{
Acquisition_Browser->Update_Header_Button->setIcon(QIcon("blueball.png"));
DICOM_Tree_Data_Changed = true;
DICOM_Tree_Data_Updated = false;
}
else
{
Acquisition_Browser->Update_Header_Button->setIcon(QIcon("greenball.png"));
DICOM_Tree_Data_Changed = false;
DICOM_Tree_Data_Updated = false;
}


if(item==DICOM_Information__Patient_Information__N ame)
{
QString Patient_Name = item->text(1);
//Acquisition_Browser->Transfer_Message->setText(item->text(1));
if(Patient_Name_Valid(Patient_Name))
{
Current_Flag_Database_Update(PATIENT_NAME_FIELD_IN VALID,false);
Acquisition_Browser->Transfer_Message->setText("Patient name is \nVALID.");
Current_Model_Index->model()->setData(*Current_Model_Index,Qt::blue,Qt::TextColo rRole);
//DICOM_Information__Patient_Information__Name->setTextColor(1,Qt::black);
}
else
{
Current_Flag_Database_Update(PATIENT_NAME_FIELD_IN VALID,true);
Acquisition_Browser->Transfer_Message->setText("Patient name is \nINVALID.");
//Current_Model_Index->setData(Current_Model_Index,Qt::blue,Qt::TextColor Role);
//item->setTextColor(0,Qt::red);
}
emit Flags_Updated();
}
}

Can anyone tell me what I am doing wrong? I feel this is something simple and I am just stressed out... either that, or my brain has a C++ memory leak.

Thanks.

jacek
1st April 2006, 19:53
QModelIndex::model() returns a pointer to a const QAbstractItemModel, so you can't invoke QAbstractItemModel::setData() (which isn't marked as const).

http://www.parashift.com/c++-faq-lite/const-correctness.html

johnny_sparx
3rd April 2006, 18:30
Ahh. The link you posted is very good.

Despite glancing over it, as well as playing around with my code, I do not see how to get around this programatically. Do you have any suggestions? Reading my initial post, and the modification I made below (lines 25 and 26):


void AcquisitionBrowserWidget::DICOM_Tree_Changed(QTree WidgetItem * item, int column )
{
if(Target_File_Path!="" || Acquisition_Browser->Read_Only_Checkbox->checkState()==Qt::Unchecked)
{
Acquisition_Browser->Update_Header_Button->setIcon(QIcon("blueball.png"));
DICOM_Tree_Data_Changed = true;
DICOM_Tree_Data_Updated = false;
}
else
{
Acquisition_Browser->Update_Header_Button->setIcon(QIcon("greenball.png"));
DICOM_Tree_Data_Changed = false;
DICOM_Tree_Data_Updated = false;
}


if(item==DICOM_Information__Patient_Information__N ame)
{
QString Patient_Name = item->text(1);
//Acquisition_Browser->Transfer_Message->setText(item->text(1));
if(Patient_Name_Valid(Patient_Name))
{
Current_Flag_Database_Update(PATIENT_NAME_FIELD_IN VALID,false);
Acquisition_Browser->Transfer_Message->setText("Patient name is \nVALID.");
const QAbstractItemModel *Model = Current_Model_Index->model();
Model->setData(*Current_Model_Index,Qt::blue,Qt::TextColo rRole);
//DICOM_Information__Patient_Information__Name->setTextColor(1,Qt::black);
}
else
{
Current_Flag_Database_Update(PATIENT_NAME_FIELD_IN VALID,true);
Acquisition_Browser->Transfer_Message->setText("Patient name is \nINVALID.");
//Current_Model_Index->setData(Current_Model_Index,Qt::blue,Qt::TextColor Role);
//item->setTextColor(0,Qt::red);
}
emit Flags_Updated();
}
}

I need to call setData to change the color. :confused: The error I get is the same as before.

wysota
3rd April 2006, 18:34
You can use const_cast<>() to cast a const pointer to a non-const one.


QAbstractItemModel *Model = const_cast<QAbstractItemModel*>(Current_Model_Index->model());

Just remember you are breaking the rule of not modifying const objects. But if you know what you're doing, feel free to do it.

EDIT: maybe in your case it'll be enough to remove the "const" keyword for the index variable?

jpn
3rd April 2006, 18:47
What's that "current model index" laying around, anyway?
Are you aware that:

Model indexes can become invalid over time so they should be used immediately and then discarded. If you need to keep a model index over time use a QPersistentModelIndex.
In my opinion, you should rather consider storing a non-const pointer to the model as a member variable or something..
One option could also be to store a a pointer to the view's selection model, which contains always an up-to-date "current index".
And why not even store a pointer to the view, from which you can access both (non-const)..

johnny_sparx
3rd April 2006, 18:58
Good point about the model indexes, in fact that is what I did without knowing.

wysota's suggestion for removing the "const" ness of the model index not only works, but is sufficient. When I remove the "const" ness of the member Current_Model_Index. const_cast Was the not-so elegant solution I was looking for.

The elegant solution was - again as wysota suggested, to remove the const keyword from the index. I am in a rush now, but will try that later on today if I have the chance.

Thank you both.

By the way, in this forum, when I click on "thanks" does that close the thread? Does it change the rating of the person receiving the thanks?

J.

wysota
3rd April 2006, 19:12
By the way, in this forum, when I click on "thanks" does that close the thread?
No.

Does it change the rating of the person receiving the thanks?

Currently only the amount of "Thanks" and "Thanked" increases.

jacek
3rd April 2006, 19:35
By the way, in this forum, when I click on "thanks" does that close the thread? Does it change the rating of the person receiving the thanks?
It's just a method of saying "Thank you" without interfering with ad rem discussion.

adrianoleal
12th July 2011, 19:30
You can use const_cast<>() to cast a const pointer to a non-const one.


QAbstractItemModel *Model = const_cast<QAbstractItemModel*>(Current_Model_Index->model());

Just remember you are breaking the rule of not modifying const objects. But if you know what you're doing, feel free to do it.

EDIT: maybe in your case it'll be enough to remove the "const" keyword for the index variable?

I'm with a similar problem, but in my case, i want do:


Model->setData(*Current_Model_Index,"Item shows this string?",Qt::DisplayRole);

And not:


Model->setData(*Current_Model_Index,Qt::blue,Qt::TextColo rRole);

I used your suggestion to remove the "const" keyword and now i can to call setData(), but it ever return me FALSE. Someone knows why?

moviemax
12th July 2011, 22:14
Hi Johnny,
at the first glance I don`t like



void AcquisitionBrowserWidget::Display_File_Path(QStrin g *File_Path_Clicked, const QModelIndex *index)

why don`t you use

void AcquisitionBrowserWidget::displayFilePath(const QString &filePath , const QModelIndex &index)
??
The const Problem is your design. Why don`t you let the objects doing there own buissnes. It`s C++ not C ;)
So the const is right and leading you to the right way
myModel->setData( ... ) should called by some delegate for instance, or

void AcquisitionBrowserWidget::updateFilePath(const QString &filePath, QModelIndex &index)

regards