Results 1 to 14 of 14

Thread: Problem with the const

  1. #1
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Problem with the const

    Dear All

    I have a function like this

    Qt Code:
    1. double form::calculate(const QString &)
    2. {
    3. id_st= id;
    4. load();
    5.  
    6. return spin->value();
    7. }
    To copy to clipboard, switch view to plain text mode 

    I try to call this function from

    Qt Code:
    1. QVariant CustomSql::data(const QModelIndex &index, int role) const
    2. {
    3.  
    4. QVariant value = QSqlTableModel::data(index, role);
    5. if (value.isValid() && role == Qt::DisplayRole) {
    6. if (index.column() == 5){
    7. calculate(index.sibling(index.row(),3).data().toString());
    8. }
    9. return value.toString().leftJustified(true);
    10. }
    11. return value;
    12. }
    To copy to clipboard, switch view to plain text mode 


    I get the error for discard qualifiers ? How can I solve this one?
    Last edited by aekilic; 8th April 2010 at 22:34.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with the const

    form::calculate() is expecting a pointer to a QString and CustomSql::data is sending it a pointer to char. Is that what you intended?

    Note: Original poster has changed the code above since this post was written.
    Last edited by ChrisW67; 8th April 2010 at 23:01. Reason: updated contents

  3. #3
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the const

    actually normaly I try to sent


    Qt Code:
    1. index.sibling(index.row(),3).data().toString()
    To copy to clipboard, switch view to plain text mode 

    but I get the error

    ... as this argument of 'double ....' discard qualifiers...

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the const

    Qt Code:
    1. "11"; // <- this is const char *
    To copy to clipboard, switch view to plain text mode 
    so you cannot pas it to method which wants a QString *. Do you really need a QString * type? Are you really changing the given string and those changes should have been visible to the caller? Then passing something like "11" really doesn't make any sense. I would say that this is an error of your class methods architecture as your method is prepared for different use then you will need. What do you want to achieve by passing pointer?

  5. #5
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the const

    just change the code, please check the message again.

    What I am doing is for a qtableview I am getting a value from 3rd row, and I send to a function in another class to calculate it. I need to send a string to calculate the value of it. In return I need this double to be on column 5th.

  6. #6
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Problem with the const

    Did he change the code snippets? Where do you see QString pointers? Or something '11'? It is very annoying to do so and does not make it easier to help. But your problem is that you try to call a non-const method from a const method. Of course this does not work.

  7. #7
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the const

    yes I know that is mistake but I need to solve it somehow

    I cant change the function calculate

  8. #8
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Problem with the const

    What you do is ugly, really ugly introducing a side-effect into data method of your model. But if you really want to do that... your funeral.

    What you can do is remove the constness of the this pointer in your data method using const_cast. cost_cast<CustomSql *>(this)->calculate....

    Good Luck

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with the const

    Quote Originally Posted by aekilic View Post
    What I am doing is for a qtableview I am getting a value from 3rd row, and I send to a function in another class to calculate it. I need to send a string to calculate the value of it. In return I need this double to be on column 5th.
    Qt Code:
    1. if (index.column() == 5){
    2. calculate(index.sibling(index.row(),3).data().toString());
    3. }
    To copy to clipboard, switch view to plain text mode 
    You are discarding the return value from the function: the result won't appear anywhere.

  10. #10
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the const

    this is why I forget to write

    normaly code is like this

    Qt Code:
    1. if (index.column() == 5){
    2. double d = form.calculate(index.sibling(index.row(),3).data().toString());
    3. return QString("%L1").arg((d),0,'f',2).leftJustified(true);
    4. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the const

    So can you paste your exact code? Because none of your snippets makes any sense. Maybe the last one is a bit more clear but still we can't deduce how form was declared and what the hell it is? :P In previous snippets you had calculate in different classes and calling it as it would be in one class... And what is:
    Qt Code:
    1. id_st=id;
    To copy to clipboard, switch view to plain text mode 
    ??
    And can you paste the exact error you get?

  12. #12
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the const

    form.h
    Qt Code:
    1. class form:public QDialog, Ui::form
    2. {
    3. Q_OBJECT
    4. public:
    5. bool isUpdate;
    6. QString totaID;
    7. form(QWidget *parent = 0
    8. , const QString &uid = QString::nulll);
    9. public slots:
    10. double calculate(const QString &id);
    11. };
    12.  
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    form.cpp

    Qt Code:
    1. double form::calculate(const QString &id)
    2. {
    3. totaID = id;
    4. load();
    5.  
    6. return spin->value();
    7. }
    To copy to clipboard, switch view to plain text mode 

    and after that I create a custom sqltablemodel

    Qt Code:
    1. QVariant CustomSql::data(const QModelIndex &index, int role) const
    2. {
    3. QVariant value = QSqlTableModel::data(index, role);
    4. if (value.isValid() && role == Qt::DisplayRole) {
    5. if (index.column() == 5){
    6. calculate(index.sibling(index.row(),3).data().toString());
    7. return QString("%L1").arg((d),0,'f',2).leftJustified(true);
    8. }
    9. return value.toString().leftJustified(true);
    10. }
    11. return value;
    12. }
    To copy to clipboard, switch view to plain text mode 


    am I clear?

  13. #13
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with the const

    No, your code doesn't make any sense. You didn't show us your CustomSql::calculate() method, and d is undefined. Also load() method is not defined. And we still don't know what is the exact error you get?

  14. #14
    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: Problem with the const

    I have a quick question for you...

    Why would you want to recalculate the value over and over again when something calls data()?

    In other words - why not calculate it once when it changes and then just return the earlier calculated value from data()?

Similar Threads

  1. Replies: 1
    Last Post: 4th December 2009, 17:03
  2. subclassed const function problem
    By qtneuling in forum Newbie
    Replies: 8
    Last Post: 22nd June 2008, 02:52
  3. const member and const method
    By mickey in forum General Programming
    Replies: 8
    Last Post: 9th April 2008, 09:44
  4. Replies: 2
    Last Post: 6th October 2006, 08:54
  5. could somebody please explain const to me?
    By mikro in forum General Programming
    Replies: 4
    Last Post: 29th September 2006, 14:34

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.