Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. QString
Forum Updated to NodeBB v4.3 + New Features

QString

Scheduled Pinned Locked Moved Qt Creator and other tools
11 Posts 4 Posters 4.7k 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.
  • M Offline
    M Offline
    muin028
    wrote on last edited by
    #1

    How can i show part of a string by QString?for example
    in c we do it like that,-----------
    @int length = 5;
    printf("%*.*s", length, length, "hello there");
    @

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

      Are you looking for QString::mid()?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        muin028
        wrote on last edited by
        #3

        thank you very much for the reply.
        i want to publish my message in plainTextEdit->appendPlainText(Qstring(a.c_str()))---------suppose my string is"1234567890",i want to show only the last 4 digit or middle one.how can i do that?

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

          Did you try reading the documentation of [[doc:QString]]?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            muin028
            wrote on last edited by
            #5

            its a large document to read.i am trying but need lots of passion.but your note helped me.now i found.thanks

            1 Reply Last reply
            0
            • M Offline
              M Offline
              muin028
              wrote on last edited by
              #6

              @std::stringstream ss1;

              uint32 stamp_1;

              ss1<stamp_1;

              QString a=ss1.str();
              @
              i am not understanding what is the wrong in my code?

              i am getting this kind of error:

              conversion from ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to non-scalar type ‘QString’ requested

              1 Reply Last reply
              0
              • M Offline
                M Offline
                muin028
                wrote on last edited by
                #7

                i got the answer.
                QString QString::fromStdString ( const std::string & str ) [static]
                Returns a copy of the str string. The given string is converted to Unicode using the fromAscii() function.

                This constructor is only available if Qt is configured with STL compatibility enabled.

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

                  I am really lost on to what you are trying to achieve. You don't need the std stuff to work with streams and strings, and not usign it would make your code more readable I think. Perhaps you should tell us what you want to achieve, rather than just showing us the concrete steps you are taking. The bigger picture is missing here.

                  Also: please put code sections between @ tags. That makes them much easier to read.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    muin028
                    wrote on last edited by
                    #9

                    I have designed a GUI Window for my project.The Gui window consist of two push button and one plain text edit.so,when i press a push button it will show the output in the plain text edit and i also using QTimer for continuous process.

                    In my project I used a time Stamp which will show the system time and uint32 primitive type.And this time i have to put in the plain text edit using QString.for this reason i have to convert my uint32 into string type.So,what i did,is that,I first convert the uint32 into streamstring then to string and finally i used
                    QString::fromStdString ( const std::string & str ) to make it suitable for QString.If you have any easy solution,please share.
                    my section of code is here:
                    @ First_Gui_Task::Num msg;
                    msg.stamp_1=ros::Time::now();

                        std::stringstream ss,ss1;
                    
                        ss1<<msg.stamp_1;
                        std::string b=ss1.str();
                        QString a(QString::fromStdString(b));
                    
                        ss << "hello world.\trequest number=["<<m_icounter<<"]";
                        msg.data_1 = ss.str();
                    
                        m_request_pub.publish(msg);
                        plainTextEdit->appendPlainText(QString("Time during send    = ") % QString(a.mid(5,11)) % QString("->Massage:: ") % QString(msg.data_1.c_str()));   @
                    
                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      Wilk
                      wrote on last edited by
                      #10

                      Hello.
                      [quote author="Andre" date="1348485098"]Did you try reading the documentation of [[doc:QString]]?[/quote]???
                      If you want to make a string from a number just use some of QString's "number":http://qt-project.org/doc/qt-4.8/qstring.html#number methods

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

                        [quote author="muin028" date="1348569443"]If you have any easy solution, please share ...[/quote]
                        Either use Qt, or the standard library, but don't use both.
                        @
                        struct Num
                        {
                        QTime stamp_1;
                        QString data_1;
                        }:

                        First_Gui_Task::Num msg = { QTime::currentTime(),
                        QString("hello world.\trequest number=[%1]").arg(m_icounter) };

                        m_request_pub.publish(msg);

                        plainTextEdit->appendPlainText(QString("Time during send = %1 -> Message:: %2")
                        .arg(msg.stamp_1.toString(Qt::ISODate))
                        .arg(msg.data_1));
                        @
                        Brain to terminal. Not tested. Exemplary.

                        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