Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Can we Typecaste QString to QAction ?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Can we Typecaste QString to QAction ?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 8 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    prabuqt
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      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().

      1 Reply Last reply
      0
      • B Offline
        B Offline
        broadpeak
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          guziemic
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            [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.

            1 Reply Last reply
            0
            • JeroentjehomeJ Offline
              JeroentjehomeJ Offline
              Jeroentjehome
              wrote on last edited by
              #6

              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

              Greetz, Jeroen

              1 Reply Last reply
              0
              • P Offline
                P Offline
                prabuqt
                wrote on last edited by
                #7

                [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.

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  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]);
                  @

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    prabuqt
                    wrote on last edited by
                    #9

                    Now got your point.
                    Thank you so much.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Macro
                      wrote on last edited by
                      #10

                      [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..

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved