QT-[Pillar of Shame] Trample Record

Error adding Q_OBJECT

In a not add Q_OBJECTthe header file, add the macro, many programs compile time error

Solution : Clean the project and rebuild.

2020.4.7

Use objects across threads

In the first UOSconducted under (a domestic Linux) Qtto develop, in the program to finish adding TCPthe heartbeat thread, it will automatically disconnect the link.

The initial operation effect is normal. When the network disconnection is detected again after the link is connected again, the program crashes and quits without error.

In fact, there is a warning about using Socket across threads, but I didn't pay attention at all... After all, it did not appear when it crashed...

Insert picture description here
Migrating to Ubuntu18.04, the same problem exists.

Finally migrated to Window, the following error prompt attracts attention:

ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x1d7cdb00. Receiver '' (of type 'QNativeSocketEngine') was created in thread 0x0x192be0f0", file kernel\qcoreapplication.cpp, line 563

That is, cross-thread access to objects.

Solution:

Finally, the Socket operation of the heartbeat thread is removed, and the heartbeat thread is changed to send a signal at a fixed time to notify the main thread to send a heartbeat packet and detect the response signal.

Of course, you can also create socket objects in the thread. Or pass in by pointer.

Summary : Threadobject itself to work in the main thread, even if the calling its definition of variables and methods, but also work in the main thread, only run( )the code within the scope of work only in the sub-thread. To avoid exceptions caused by cross-thread calls, the creation and call of an object should be placed in the same thread.

2020.4.15

txt file save Chinese incomplete problem

Let's take a look at how I wrote the original file save:

        QString saveFile = "";
        //借助QFileDialog获取保存路径及文件名
        saveFile = QFileDialog::getSaveFileName(this,"保存","","文本文件(*.txt)");
        QString filepath = QFileInfo(saveFile).absolutePath();
        QString filename = QFileInfo(saveFile).fileName();
        
        QDir mDir;
        //保存当前路径
        QString currentDir = mDir.currentPath();
        
        QFile *tempFile = new QFile();
        //设置保存路径
        mDir.setCurrent(filepath);
        
        tempFile->setFileName(filename);
        if(tempFile->open(QIODevice::WriteOnly|QIODevice::Text)){
    
    
            QString wStr = ui->textBrowser_intput->document()->toPlainText();
            tempFile->write(wStr.toLocal8Bit().data(),wStr.length());
            tempFile->close();
        }
        else{
    
    
            setNewsColor(Qt::red);
            mlaybelNews->setText("Save File Error!");
        }
        setNewsColor(Qt::black);
        mlaybelNews->setText("Save File Success!");
        //程序路径复原
        mDir.setCurrent(currentDir);
    }

It can be seen using write( )this method to specify a char*and 数据长度.

It looks so dripping, is it really true?

Once the Chinese appear or text, using this method because the default save format is GBKan 汉字accounting 两个字节. So the calculated length here is wrong. This saving will cause data incompleteness.

Solution :

With the help of QTextStream

            QTextStream streamFileOut(tempFile);
            streamFileOut.setCodec("UTF-8");
            streamFileOut << wStr;
            streamFileOut.flush();

2020.5.17

Guess you like

Origin blog.csdn.net/weixin_40774605/article/details/105542546