Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Convert QString pointer to char pointer ?



  • 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


  • Lifetime Qt Champion

    Hi,

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



  • @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.


  • Moderators

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



  • @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 ????


  • Moderators

    @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."



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


  • Moderators

    @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

  • Moderators

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


Log in to reply