Questions about QMainWindow and QPlainTextEdit



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


  • Lifetime Qt Champion

    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



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



  • @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);
        }
    


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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.