Results 1 to 10 of 10

Thread: Having trouble interpreting an error message. Is my C++ that bad?

  1. #1
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Arrow Having trouble interpreting an error message. Is my C++ that bad?

    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):

    Qt Code:
    1. void AcquisitionBrowserWidget::Display_File_Path(QString *File_Path_Clicked, const QModelIndex *index)
    2. {
    3. Acquisition_Browser->MainFilePathDisplay->setText(*File_Path_Clicked);
    4. // Update the internal target file path.
    5. Target_File_Path = *File_Path_Clicked;
    6. Current_Model_Index = index;
    7. Update_All_Browser_Information();
    8. //Acquisition_Browser->Transfer_Progress_Bar->setValue(0);
    9. }
    To copy to clipboard, switch view to plain text mode 

    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::TextCol orRole);

    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:ICOM_Tree_Changed(QTreeWidgetItem*, 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:ICOM_Tree_Item_Double_Clicked(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:
    Qt Code:
    1. void AcquisitionBrowserWidget::DICOM_Tree_Changed(QTreeWidgetItem * item, int column )
    2. {
    3. if(Target_File_Path!="" || Acquisition_Browser->Read_Only_Checkbox->checkState()==Qt::Unchecked)
    4. {
    5. Acquisition_Browser->Update_Header_Button->setIcon(QIcon("blueball.png"));
    6. DICOM_Tree_Data_Changed = true;
    7. DICOM_Tree_Data_Updated = false;
    8. }
    9. else
    10. {
    11. Acquisition_Browser->Update_Header_Button->setIcon(QIcon("greenball.png"));
    12. DICOM_Tree_Data_Changed = false;
    13. DICOM_Tree_Data_Updated = false;
    14. }
    15.  
    16.  
    17. if(item==DICOM_Information__Patient_Information__Name)
    18. {
    19. QString Patient_Name = item->text(1);
    20. //Acquisition_Browser->Transfer_Message->setText(item->text(1));
    21. if(Patient_Name_Valid(Patient_Name))
    22. {
    23. Current_Flag_Database_Update(PATIENT_NAME_FIELD_INVALID,false);
    24. Acquisition_Browser->Transfer_Message->setText("Patient name is \nVALID.");
    25. Current_Model_Index->model()->setData(*Current_Model_Index,Qt::blue,Qt::TextColorRole);
    26. //DICOM_Information__Patient_Information__Name->setTextColor(1,Qt::black);
    27. }
    28. else
    29. {
    30. Current_Flag_Database_Update(PATIENT_NAME_FIELD_INVALID,true);
    31. Acquisition_Browser->Transfer_Message->setText("Patient name is \nINVALID.");
    32. //Current_Model_Index->setData(Current_Model_Index,Qt::blue,Qt::TextColorRole);
    33. //item->setTextColor(0,Qt::red);
    34. }
    35. emit Flags_Updated();
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having trouble interpreting an error message. Is my C++ that bad?

    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-lit...rrectness.html

  3. #3
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: Having trouble interpreting an error message. Is my C++ that bad?

    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):

    Qt Code:
    1. void AcquisitionBrowserWidget::DICOM_Tree_Changed(QTreeWidgetItem * item, int column )
    2. {
    3. if(Target_File_Path!="" || Acquisition_Browser->Read_Only_Checkbox->checkState()==Qt::Unchecked)
    4. {
    5. Acquisition_Browser->Update_Header_Button->setIcon(QIcon("blueball.png"));
    6. DICOM_Tree_Data_Changed = true;
    7. DICOM_Tree_Data_Updated = false;
    8. }
    9. else
    10. {
    11. Acquisition_Browser->Update_Header_Button->setIcon(QIcon("greenball.png"));
    12. DICOM_Tree_Data_Changed = false;
    13. DICOM_Tree_Data_Updated = false;
    14. }
    15.  
    16.  
    17. if(item==DICOM_Information__Patient_Information__Name)
    18. {
    19. QString Patient_Name = item->text(1);
    20. //Acquisition_Browser->Transfer_Message->setText(item->text(1));
    21. if(Patient_Name_Valid(Patient_Name))
    22. {
    23. Current_Flag_Database_Update(PATIENT_NAME_FIELD_INVALID,false);
    24. Acquisition_Browser->Transfer_Message->setText("Patient name is \nVALID.");
    25. const QAbstractItemModel *Model = Current_Model_Index->model();
    26. Model->setData(*Current_Model_Index,Qt::blue,Qt::TextColorRole);
    27. //DICOM_Information__Patient_Information__Name->setTextColor(1,Qt::black);
    28. }
    29. else
    30. {
    31. Current_Flag_Database_Update(PATIENT_NAME_FIELD_INVALID,true);
    32. Acquisition_Browser->Transfer_Message->setText("Patient name is \nINVALID.");
    33. //Current_Model_Index->setData(Current_Model_Index,Qt::blue,Qt::TextColorRole);
    34. //item->setTextColor(0,Qt::red);
    35. }
    36. emit Flags_Updated();
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Having trouble interpreting an error message. Is my C++ that bad?

    You can use const_cast<>() to cast a const pointer to a non-const one.

    Qt Code:
    1. QAbstractItemModel *Model = const_cast<QAbstractItemModel*>(Current_Model_Index->model());
    To copy to clipboard, switch view to plain text mode 

    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?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Having trouble interpreting an error message. Is my C++ that bad?

    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)..
    J-P Nurmi

  6. #6
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having trouble interpreting an error message. Is my C++ that bad?

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Having trouble interpreting an error message. Is my C++ that bad?

    Quote Originally Posted by johnny_sparx
    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having trouble interpreting an error message. Is my C++ that bad?

    Quote Originally Posted by johnny_sparx
    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.

  9. #9
    Join Date
    Feb 2011
    Posts
    10
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Having trouble interpreting an error message. Is my C++ that bad?

    Quote Originally Posted by wysota View Post
    You can use const_cast<>() to cast a const pointer to a non-const one.

    Qt Code:
    1. QAbstractItemModel *Model = const_cast<QAbstractItemModel*>(Current_Model_Index->model());
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. Model->setData(*Current_Model_Index,"Item shows this string?",Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

    And not:

    Qt Code:
    1. Model->setData(*Current_Model_Index,Qt::blue,Qt::TextColorRole);
    To copy to clipboard, switch view to plain text mode 

    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?

  10. #10
    Join Date
    Jun 2010
    Location
    Germany, Munich
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Having trouble interpreting an error message. Is my C++ that bad?

    Hi Johnny,
    at the first glance I don`t like
    Qt Code:
    1. void AcquisitionBrowserWidget::Display_File_Path(QString *File_Path_Clicked, const QModelIndex *index)
    To copy to clipboard, switch view to plain text mode 
    why don`t you use
    Qt Code:
    1. void AcquisitionBrowserWidget::displayFilePath(const QString &filePath , const QModelIndex &index)
    To copy to clipboard, switch view to plain text mode 
    ??
    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
    Qt Code:
    1. void AcquisitionBrowserWidget::updateFilePath(const QString &filePath, QModelIndex &index)
    To copy to clipboard, switch view to plain text mode 

    regards

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.