qt-window print debug information, local log save, and execute shell script and print the information in the window

Source: Internet
Author: User

Haven't written a blog for a long time, recently work needs to study the following features:

1: Print the Qdebug information to the QT window

2: Save Qdebug information to local

3: Execute shell Script

4: Output terminal information to QT window


First on the code:

#include "logbrowser.h"
#include <QApplication>
#include <QPointer>
#include <qdebug >

qpointer<logbrowser> log_broswer;

 void Mymessageoutput (Qtmsgtype type, const char *msg)
{
    if (log_broswer)
    log_broswer->outputmessage ( Type, msg);
}


int main (int argc, char *argv[])
{
    qapplication A (argc, argv);

    Log_broswer = new Logbrowser;
    Log_broswer->show ();
    Qinstallmsghandler (mymessageoutput);
    int result = A.exec ();
    Delete Log_broswer;

    return result;
}



#ifndef logbrowser_h #define Logbrowser_h #include <QWidget> #include <QTextBrowser> #include < qpushbutton> #include <QHBoxLayout> #include <QVBoxLayout> #include <QtDebug> #include < qmessagebox> #include <QCloseEvent> #include <QProcess> #include <QFileSystemWatcher> #include

<QDir> #include <QTimer> namespace Ui {class Logbrowser;}
    Class Logbrowser:public Qwidget {q_object public:explicit logbrowser (qwidget *parent = 0);
    ~logbrowser ();


void Outputmessage (Qtmsgtype type, const QString &msg);
    Public slots:void start ();
    void Save (bool enable);  void directoryupdated (const QString &path);
    Called when directory update, path is the path of monitoring private slots:void on_pushbutton_start_clicked ();
    void on_pushbutton_stop_clicked ();
    void on_pushbutton_save_clicked ();

    void on_pushbutton_exit_clicked ();
    void Autoupdata ();
    void on_pushbutton_updata_clicked (); void DISPLAYUDISKFIlelist ();

void Readcmdinformation ();
    Private:ui::logbrowser *ui;
    Qtextbrowser *browser;
    Qpushbutton *start_button;
    Qpushbutton *clear_button;
    Qprocess *my_process;
    Qfilesystemwatcher *my_syswatcher;
    Qtimer *my_timer;
    BOOL is_finished;


BOOL my_saveenable;

};
 #endif//Logbrowser_h




#include "logbrowser.h" #include "ui_logbrowser.h" Logbrowser::logbrowser (Qwidget *parent): Qwidget (parent), UI (

    New Ui::logbrowser) {ui->setupui (this);
    Background transparent Setautofillbackground (false);
    Setwindowflags (Qt::framelesswindowhint);


    SetAttribute (qt::wa_translucentbackground,true);
    My_timer = new Qtimer ();                my_process = new Qprocess (); The process of executing the command//when there is output, a message is emitted.

    The receive slot reads the data in the process pipeline connect (my_process,signal (Readyread ()), This,slot (Readcmdinformation ()));
    My_syswatcher = new Qfilesystemwatcher ();             My_syswatcher->addpath ("/mnt"); Monitor folder path//If the folder changes, notify Connect (My_syswatcher,signal (directorychanged (QString)), This,slot (directoryupdated (QS

    (Tring)));


    Ui->pushbutton_updata->setenabled (FALSE);
    Is_finished = false;
My_saveenable = false;

} logbrowser::~logbrowser () {Delete UI; }

//! Read the command window information, displayed on its own window void Logbrowser::readcmdinformation () {QString str = My_process->readAllstandardoutput ();
    Ui->plaintextedit->appendplaintext (str);
Ui->plaintextedit->movecursor (Qtextcursor::end);
    }//! receive debug underlying information void Logbrowser::outputmessage (qtmsgtype type, const QString &msg) {QString message;
            Switch (type) {Case qtdebugmsg:message = QString ("Debug:");

        Break
            Case qtwarningmsg:message = QString ("Warning:");

        Break
            Case qtcriticalmsg:message = QString ("Critical:");

        Break
            Case qtfatalmsg:message = QString ("Fatal:");
    Break
    } ui->plaintextedit->appendplaintext (Message.append (msg));  Ui->plaintextedit->movecursor (Qtextcursor::end);
        The slide bar moves to the end if (my_saveenable) {QFile file ("Log.txt"); File.Open (Qiodevice::writeonly |
        Qiodevice::append);
        Qtextstream Text_stream (&file);
        Text_stream << message << "\ r \ n"; FiLe.flush ();
    File.close ();
        }}//A test function, when normal use, you can delete void Logbrowser::start () {for (int i=0; i<1000000; i++) {if (!is_finished)
            {qcoreapplication::p rocessevents ();
        Qdebug () <<qstring ("Qdebug::"). Append (Qstring::number (i, 10));
        }else{return;

}}}//! open the save switch, all debug information is saved to the Log.txt file void Logbrowser::save (bool enable) {my_saveenable = enable;} //!
Automatic upgrade, that is, automatic execution of scripts. void Logbrowser::autoupdata () {ui->plaintextedit->appendplaintext ("Auto updata start!!!
    \ r \ n ");
   My_process->start ("/mnt/sda1/auto.sh");
My_process->start ("/home/lbs_work/test.sh"); }

//!
   When a USB flash drive is inserted, read the file directory and open the update void Logbrowser::d irectoryupdated (const QString &path) {const Qdir dir (path); Qstringlist newentrylist = dir.entrylist (Qdir::nodotanddotdot | Qdir::alldirs |

    Qdir::files, Qdir::D irsfirst); if (Newentrylist.contains ("sda1")) {Ui->plaintextedit->setplaintext("U disk has been mounted");
        Emit My_timer->singleshot (2000,this,slot (Displayudiskfilelist ()));
    Ui->pushbutton_updata->setenabled (TRUE);
        }else{ui->plaintextedit->setplaintext ("U disk has been unmounted");
    Ui->pushbutton_updata->setenabled (FALSE); }

}

//!
    Displays the USB Stick folder directory, here needs the delay, because just mounts the USB flash drive, reads immediately is not to read anything the void Logbrowser::d isplayudiskfilelist () {const Qdir udir ("/mnt/sda1"); Qstringlist udisklist = udir.entrylist (Qdir::nodotanddotdot | Qdir::alldirs |

    Qdir::files, Qdir::D irsfirst);
    Ui->plaintextedit->appendplaintext (QString (). Setnum (Udisklist.length ()));
    for (int i = 0; i < udisklist.length (); ++i) {Ui->plaintextedit->appendplaintext (udisklist[i]);
   }} void Logbrowser::on_pushbutton_start_clicked () {is_finished = false;
This->start ();

} void Logbrowser::on_pushbutton_stop_clicked () {is_finished = true;} void logbrowser::on_pushbutton_save_clicked () {this-&Gt;save (TRUE);
    } void Logbrowser::on_pushbutton_exit_clicked () {this->close (); Delete this;



 This is the focus} void Logbrowser::on_pushbutton_updata_clicked () {this->autoupdata ();}

It is important to note the hierarchical relationship between classes and classes.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.