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. Convert QString pointer to char pointer ?

Convert QString pointer to char pointer ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.6k Views
  • 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on 21 Mar 2021, 21:04 last edited by
    #1

    Is there cleaner Qt way ?

    int MainWindow::createMenu(QString menuName )
    {
    std::string str = menuName->toStdString();
    const char
    p = str.c_str();

    // add (main) new main menu to menuBar
    

    "tr" wants char *

    QMenu *fileMenu_NEW = menuBar()->addMenu(tr(p));
    

    .....\

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 21 Mar 2021, 22:41 last edited by
      #2

      Hi,

      You are calling tr at the wrong place. It should where you actually have the text in your code,

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 22 Mar 2021, 00:19
      4
      • S SGaist
        21 Mar 2021, 22:41

        Hi,

        You are calling tr at the wrong place. It should where you actually have the text in your code,

        A Offline
        A Offline
        Anonymous_Banned275
        wrote on 22 Mar 2021, 00:19 last edited by
        #3

        @SGaist said in Convert QString pointer to char pointer ?:

        Hi,

        You are calling tr at the wrong place. It should where you actually have the text in your code,

        It is not my choice. It is just "cut and paste " from MDI example . I am not at the point / not interested of figuring out why "tr" is actually used in the example.
        But it is there...and doing that silly "backwards" i conversion multiple times is boring.

        J 1 Reply Last reply 22 Mar 2021, 00:36
        0
        • A Anonymous_Banned275
          22 Mar 2021, 00:19

          @SGaist said in Convert QString pointer to char pointer ?:

          Hi,

          You are calling tr at the wrong place. It should where you actually have the text in your code,

          It is not my choice. It is just "cut and paste " from MDI example . I am not at the point / not interested of figuring out why "tr" is actually used in the example.
          But it is there...and doing that silly "backwards" i conversion multiple times is boring.

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 22 Mar 2021, 00:36 last edited by JKSH
          #4

          @AnneRanch said in Convert QString pointer to char pointer ?:

          It is not my choice. It is just "cut and paste " from MDI example .

          The MDI example only uses tr() with string literals. It never uses tr() with a converted QString.

          I am not at the point / not interested of figuring out why "tr" is actually used in the example.

          Then don't use tr(). Pass your QString into addMenu() directly. Doing so makes your problem disappear, since you won't need a char pointer.

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

          1 Reply Last reply
          7
          • A Offline
            A Offline
            Anonymous_Banned275
            wrote on 22 Mar 2021, 03:59 last edited by
            #5

            @JKSH said in Convert QString pointer to char pointer ?:

            Doing so makes your problem disappear, since you won't need a char pointer.
            Convert QString pointer to char pointer ????

            J 1 Reply Last reply 22 Mar 2021, 05:45
            0
            • A Anonymous_Banned275
              22 Mar 2021, 03:59

              @JKSH said in Convert QString pointer to char pointer ?:

              Doing so makes your problem disappear, since you won't need a char pointer.
              Convert QString pointer to char pointer ????

              J Offline
              J Offline
              JKSH
              Moderators
              wrote on 22 Mar 2021, 05:45 last edited by JKSH
              #6

              @AnneRanch said in Convert QString pointer to char pointer ?:

              @JKSH said in Convert QString pointer to char pointer ?:

              Doing so makes your problem disappear, since you won't need a char pointer.

              Convert QString pointer to char pointer ????

              Are you still asking how to get a char pointer? What for?

              Question: "Is there cleaner Qt way?"
              Answer: "Yes, the cleaner Qt way is avoid the char pointer. Remove tr() from your code and pass menuName directly into addMenu()."

              Question: "Is there a cleaner Qt way to convert the contents of a QString into a char pointer and pass that into tr()?"
              Answer: "No, there is no meaningful way at all to convert a QString into a char pointer and pass it into tr()."

              Question: "Is there a cleaner Qt way to get a char pointer from a QString for non-tr() purposes?"
              Answer: "No, QString::toStdString() and std::string::c_str() are the bare minimum."

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

              1 Reply Last reply
              6
              • S Offline
                S Offline
                SimonSchroeder
                wrote on 23 Mar 2021, 07:44 last edited by
                #7

                There is not much to add to what @JKSH already said. Specifically, tr() is for translation. If you don't do translation or you don't know about tr() at all, you can savely drop it from your code and it won't change a thing.

                One thing I'd like to add: In our code instead of QString::toStdString() followed by std::string::c_str() we use QString::toUtf8() followed by QByteArray::data(). This solution came about because at some point we had problems with some destructor calls of std::~string while transitioning to C++11.

                Under certain circumstance (which are still not clear to me) it is necessary to use a temporary variable for the QByteArray (and I assume it would be the same with std::string in the other solution). We had some very few curious crashes when converting a QString directly to const char * and passing it to functions (all in the same line). Just keep this in mind if you do run into problems somewhere. This error can rarely be forced (most of the time you are lucky anyway).

                J 1 Reply Last reply 23 Mar 2021, 07:50
                3
                • S SimonSchroeder
                  23 Mar 2021, 07:44

                  There is not much to add to what @JKSH already said. Specifically, tr() is for translation. If you don't do translation or you don't know about tr() at all, you can savely drop it from your code and it won't change a thing.

                  One thing I'd like to add: In our code instead of QString::toStdString() followed by std::string::c_str() we use QString::toUtf8() followed by QByteArray::data(). This solution came about because at some point we had problems with some destructor calls of std::~string while transitioning to C++11.

                  Under certain circumstance (which are still not clear to me) it is necessary to use a temporary variable for the QByteArray (and I assume it would be the same with std::string in the other solution). We had some very few curious crashes when converting a QString directly to const char * and passing it to functions (all in the same line). Just keep this in mind if you do run into problems somewhere. This error can rarely be forced (most of the time you are lucky anyway).

                  J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 23 Mar 2021, 07:50 last edited by JKSH
                  #8

                  @SimonSchroeder said in Convert QString pointer to char pointer ?:

                  Under certain circumstance (which are still not clear to me) it is necessary to use a temporary variable for the QByteArray (and I assume it would be the same with std::string in the other solution).

                  You must always store the intermediate data in a variable until you no longer need the char*. Otherwise, you end up with an invalid, dangling pointer which points to memory that has been freed.

                  If you're lucky, you get a crash straight away so you know that something's wrong. If you're unlucky, you don't notice anything until much later.

                  QString qstr = ...
                  
                  const char *p1 = qstr.toStdString().c_str(); // The std::string gets destroyed after this line
                  // p1 is now a dangling pointer
                  
                  const char *p2 = qstr.toUtf8().constData(); // The QByteArray gets destroyed after this line
                  // p2 is now a dangling pointer

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

                  K 1 Reply Last reply 23 Mar 2021, 16:16
                  5
                  • J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 23 Mar 2021, 21:31 last edited by
                    #9

                    [Additional discussion about temporary object lifetime is forked to https://forum.qt.io/topic/125012/lifetime-of-temporary-objects ]

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

                    1 Reply Last reply
                    2
                    • C Christian Ehrlicher referenced this topic on 15 Mar 2023, 17:49

                    9/9

                    23 Mar 2021, 21:31

                    • Login

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