Search code examples
c++qt5qtextbrowser

QTextBrowser could not show all data at run time


My problem is that QTextBrowser can not show all data that is appended to it. I am using qt5.4 on windows. My class like this:

class InfoTextBrowser : public QTextBrowser
{
    Q_OBJECT

public:
    InfoTextBrowser(QObject *parent);
    ~InfoTextBrowser();
public slots:
        void appendText(const QString& text) {

            try
            {

                this->append(text); 
            }
            catch(std::bad_alloc& e)
            {
                if(!memoryError)
                {
                    QMessageBox::warning(this,"My app", QStringLiteral("Out of Memory"),QMessageBox::Ok);
                    this->clear();
                    memoryError = true;
                }
            }

            QScrollBar* myBar = this->verticalScrollBar();
            if (myBar!=NULL)
                myBar->setValue(myBar->maximum());  
        };
private:
     void contextMenuEvent(QContextMenuEvent *event);   
private:
    bool memoryError;
};

First I got bad_alloc exception and then I moved my application to 64bit then I do not get any exception. But When I run my program, QTextBrowser stops showing data after a while. It dies and it can not be cleared or refreshed.

I also tried QPlainTextEdit instead of this, but I could not be successful again. I want to show all data at run time or some part of it that could be shown. Because it stops just first paragraph when it dies. In addition, I do not want to show data by reading from a text file.

Is there anyone face with this situation?

Thanks in advance.


Solution

  • Actually, I decided that there is no feasible solution for this, you can not hold this data at run time, It very depends on hardware. You do not have a guarantee to find the needed memory as a whole block.

    So I used QPlainTextEdit and restricted to shown text as 100000 blocks by using setMaximumBlockCount(100000)function in the constructor. I appended text with this->appendPlainText(text). Thus,output windows always shows last 100000 block of text. Rest of data can hold a text file or something like that. So user can look all of them from there.