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. Questions about QMainWindow and QPlainTextEdit
QtWS25 Last Chance

Questions about QMainWindow and QPlainTextEdit

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.5k 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.
  • C Offline
    C Offline
    Clint Westwood
    wrote on last edited by Clint Westwood
    #1

    Using widgets.

    1. I'd like my application to appear in the center of screen. I can do that from C++ but I want to do that from Qt Creator, since I like it when GUI controls are separated from the rest of program. How do I do it?

    2. I've coded a packet sniffer that outputs data to QPlainTextEdit. It outputs a lot of data. It deals just fine with around hundreds of packets per second but is having problems if there are thousands of packets per second to the point where GUI has problems with response. I realize I can use other model/view paradigms to deal with that but I'm just curious if I can squeezee a bit more performance from this widget type.

    Program works like this:

    main thread (ui) ------------> qthread --------------> endless loop (sniffer) 
    

    Communication between child thread and main thread is done like this:

    (slot that receives Qstring)<---main thread<---(signal with QString)<---sniffer thread 
    

    I have two ways of doing this at the moment:

    1. Complete QString for a single packet in child thread and pass that big chunk of text to main thread at the end of loop iteration, then repeat it for the next packet and so on.
    2. Constantly pass singnals with smaller chunks of text from child thread to main thread.

    I'm using option 1 at the moment but I'm not sure if it's worth it to rewrite code just to test option 2. Which is faster: less signals to main thread with more data or more signals to main thread with less data? In general and with QPlainTextEdit in particular.

    Are there any other solutions? What would give the best performance?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It's not really a question of number of slots call but updating the GUI itself. You should try "batch" updates i.e. you store the data received and update the QPlainTextEdit on a regular interval e.g. twice per second

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        ki John
        wrote on last edited by ki John
        #3

        For question 1:
        Watch this video. Pay attention a minute 2:17s. User clicks the Layout Vertically button.
        VIDEO - C++ Qt 06 - layouts, tabs and buddies

        For question 2:
        You are probably not giving the main thread a chance to refresh the UI widgets. It must be a QueuedConnection.
        Actually, can you show the code? How are you sending the signal to the main thread from the child thread? This scenario is one of the greatest pitfalls of Qt5 since they changed the threading/signalling architecture.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Clint Westwood
          wrote on last edited by
          #4

          @SGaist said:

          Hi,

          It's not really a question of number of slots call but updating the GUI itself. You should try "batch" updates i.e. you store the data received and update the QPlainTextEdit on a regular interval e.g. twice per second

          Where should I put the delay? Putting it in child thread would disrupt program flow. I guess I should do that inside slot, right?

          @ki-John said:

          For question 1:
          Watch this video. Pay attention a minute 2:17s. User clicks the Layout Vertically button.
          VIDEO - C++ Qt 06 - layouts, tabs and buddies

          This only changes the layout but doesn't move Main Window to the center of the screen

          @ki-John said:

          For question 2:
          You are probably not giving the main thread a chance to refresh the UI widgets. It must be a QueuedConnection.
          Actually, can you show the code? How are you sending the signal to the main thread from the child thread? This scenario is one of the greatest pitfalls of Qt5 since they changed the threading/signalling architecture.

          Can't post code at the moment, but this is roughly how my threads are communicating:

          This is how I establish signal-slot connection in main function:

           QObject::connect(&Mainwindow, SIGNAL(send_output(QString)),
                            &Mainwindow, SLOT(receive_output (QString)));
          
          

          This is how function from child thread communicates with main thread:

          void child_thread ()
          {
              //some code
              while (some_boolean)
              {
                   // various code to form QString
                   // approaching the end of current iteration, time to send data to main
                   pointer_to_Main_thread->send_output(QString_from_child_thread);
                   // data sent, repeat cycle again.
              }
          }
          
          

          And this is how I define my slot:

              void receive_output (QString QString_from_child_thread)
              {
                  ui->myQPlainTextEdit->appendPlainText(QString_from_child_thread);
              }
          
          1 Reply Last reply
          0
          • K Offline
            K Offline
            ki John
            wrote on last edited by
            #5

            Oh for centering the MainWindow here's a good example:
            link text

            choose the first link - it's the best.

            Basically it takes the size of the desktop from the primary monitor and it finds the horizontal half and the vertical half.

            If you have an array of 9 or 12 monitors then you have a problem.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Clint Westwood
              wrote on last edited by
              #6

              @ki-John said:

              Oh for centering the MainWindow here's a good example:
              link text

              choose the first link - it's the best.

              I've been using this example already, works well. I was just wondering if same thing could be done in form editor of Qt Creator. I'm trying to do all work on graphics from there but from time to time I run into minor limitations and have to do some form editing from C++

              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