[SOLVED]Can we Typecaste QString to QAction ?



  • HI everyone,

    I tried to typecast Qstring to QAction by the following code. But it is not working.
    @QString Str1="Profile";
    QAction *act = (QAction *)Str1;@

    Can you give me the correct method to typecast?
    Thank you



  • Does an apple become a banana just because you paint it yellow? Most probably not.

    The same way you can't cast a QString to a QAction.

    If you want to create a QAction with a given text you will have to construct a QAction object and set the text, by either passing it to the constructor or using setText().



  • I think, even the reinterpret_cast will fail in this case...



  • bq. I think, even the reinterpret_cast will fail in this case…

    Even if able to compile it is not safe way to solve problem.



  • [quote author="prabuqt" date="1349785227"]HI everyone,

    I tried to typecast Qstring to QAction by the following code. But it is not working.
    @QString Str1="Profile";
    QAction *act = (QAction *)Str1;@

    Can you give me the correct method to typecast?
    Thank you[/quote]

    This just doesn't make any sense. None at all. So no, there is no way to cast the one to the other, and it would never make sense.



  • Hi there,
    Like said before, you just can't cast it that way. casting is only done with lvalue types not with complete classes.
    Why do you want to use the casting? What do you want to achieve? We might help better then.
    Greetz



  • [quote author="Jeroentje@home" date="1349849595"]Hi there,
    Like said before, you just can't cast it that way. casting is only done with lvalue types not with complete classes.
    Why do you want to use the casting? What do you want to achieve? We might help better then.
    Greetz[/quote]
    Thank you for your posts. Here what I wanted to do is, I want to create the QMenuBar dynamically, for that I am having the a set of strings in QStringList. So I just tried to convert the QStrings to QActions. Now I compleated this task by using QHash<QString,int> and switch statement.
    Thank you all for giving the replay.


  • Moderators

    Lukas already gave you the answer. If you still want to try QAction:
    [quote author="Lukas Geyer" date="1349789428"]If you want to create a QAction with a given text you will have to construct a QAction object and set the text, by either passing it to the constructor or using setText().[/quote]

    @
    QStringList actionList;
    actionList << "Action 1" << "Action 2";

    // Passing to the constructor
    QAction *action1 = new QAction(actionList[0], this);

    // Using setText()
    QAction *action2 = new QAction(this);
    action2->setText(actionList[1]);
    @



  • Now got your point.
    Thank you so much.



  • [quote author="Lukas Geyer" date="1349789428"]Does an apple become a banana just because you paint it yellow? Most probably not.

    The same way you can't cast a QString to a QAction.

    If you want to create a QAction with a given text you will have to construct a QAction object and set the text, by either passing it to the constructor or using setText().[/quote]

    Awesome Example.. :) Great..


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.