我有一个QT应用程序,它将qDebug()输出导出到文件中。为此,我在主文件中使用了它:
已编辑(添加了有效的应用程序)
主要:
#include "mainwindow.h"
#include <QApplication>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QStandardPaths>
#include <QVector>
#include <QDateTime>
#include <QDebug>
void log_message(QtMsgType type, const QMessageLogContext& context, const QString &msg)
{
QString timeStamp(QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss.zzz"));
QString app_data_path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
QString log_path = app_data_path + "/log/myapp.log";
#ifdef QT_DEBUG
log_path = "myapp.log";
#endif
QFile log_file(log_path);
if(log_file.open(QIODevice::WriteOnly | QIODevice::Append))
{
QString final_msg;
if (log_file.size() > 10000000) //If size is > 10MB, truncate file
{
log_file.close();
log_file.open(QIODevice::WriteOnly);
}
switch (type) {
case QtDebugMsg:
final_msg = timeStamp.toUtf8() + " - Debug: " + msg.toUtf8() + "\n";
log_file.write(final_msg.toUtf8(), final_msg.size());
break;
case QtWarningMsg:
final_msg = timeStamp.toUtf8() + " - Warning: " + msg.toUtf8()+ "\n";
log_file.write(final_msg.toUtf8(), final_msg.size());
break;
case QtCriticalMsg:
final_msg = timeStamp.toUtf8() + " - Critical: " + msg.toUtf8()+ "\n";
log_file.write(final_msg.toUtf8(), final_msg.size());
break;
case QtFatalMsg:
final_msg = timeStamp.toUtf8() + " - Fatal: " + msg.toUtf8()+ "\n";
log_file.write(final_msg.toUtf8(), final_msg.size());
abort();
}
}
log_file.close();
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(log_message);
qDebug() << "Starting app";
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
w.show_login();
return a.exec();
}
login_dialog.cpp
#include "login_dialog.h"
#include "ui_login_dialog.h"
#include <QDialogButtonBox>
#include <QPushButton>
#include <QDebug>
#include <QLabel>
#include <QSpacerItem>
#include <iostream>
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
this->setWindowTitle("Log In");
this->setWindowModality(Qt::ApplicationModal); //Blocks all interaction outside this window
//Login Layout
_login_form_layout = new QFormLayout;
_login_email_edit = new QLineEdit;
_login_form_layout->addRow("Email:", _login_email_edit);
_login_password_edit = new QLineEdit;
_login_password_edit->setEchoMode(QLineEdit::Password);
_login_form_layout->addRow("Contraseña:", _login_password_edit);
QLabel *forgot_password_label = new QLabel();
forgot_password_label->setText("<a href=\"http://localhost\">Has olvidado la contraseña?</a>");
forgot_password_label->setTextInteractionFlags(Qt::TextBrowserInteraction);
QObject::connect(forgot_password_label,
SIGNAL(linkActivated(QString)),
this,
SLOT(forgot_password(QString)));
_login_form_layout->addWidget(forgot_password_label);
QPushButton *login_button = new QPushButton("Login");
QObject::connect(login_button,
SIGNAL(clicked()),
this,
SLOT(validate_login()));
QLabel *no_account_label = new QLabel("No tienes una cuenta?");
no_account_label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QPushButton *go_to_signup_button = new QPushButton("Regístrate");
QObject::connect(go_to_signup_button,
SIGNAL(clicked()),
this,
SLOT(change_page()));
no_account_label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QGridLayout *go_to_signup_button_layout = new QGridLayout;
go_to_signup_button_layout->addWidget(no_account_label,0,0, Qt::AlignCenter);
go_to_signup_button_layout->addWidget(go_to_signup_button,1,0, Qt::AlignCenter);
QVBoxLayout *main_login_layout = new QVBoxLayout;
main_login_layout->addLayout(_login_form_layout);
main_login_layout->addWidget(login_button);
main_login_layout->addStretch();
main_login_layout->addLayout(go_to_signup_button_layout);
QWidget *login_container = new QWidget;
login_container->setLayout(main_login_layout);
//SignUp layout
_signup_form_layout = new QFormLayout;
_signup_email_edit = new QLineEdit;
_signup_form_layout->addRow("Email:", _signup_email_edit);
_signup_password_edit = new QLineEdit;
_signup_password_edit->setEchoMode(QLineEdit::Password);
_signup_form_layout->addRow("Contraseña:", _signup_password_edit);
QPushButton *signup_button = new QPushButton("Regístrate");
QObject::connect(signup_button,
SIGNAL(clicked()),
this,
SLOT(validate_signup()));
QLabel *already_account_label = new QLabel("Ya tienes una cuenta?");
already_account_label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QPushButton *go_to_login_button = new QPushButton("Log In");
QObject::connect(go_to_login_button,
SIGNAL(clicked()),
this,
SLOT(change_page()));
already_account_label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QGridLayout *go_to_login_button_layout = new QGridLayout;
go_to_login_button_layout->addWidget(already_account_label,0,0, Qt::AlignCenter);
go_to_login_button_layout->addWidget(go_to_login_button,1,0, Qt::AlignCenter);
QVBoxLayout *main_signup_layout = new QVBoxLayout;
main_signup_layout->addLayout(_signup_form_layout);
main_signup_layout->addWidget(signup_button);
main_signup_layout->addStretch();
main_signup_layout->addLayout(go_to_login_button_layout);
QWidget *signup_container = new QWidget;
signup_container->setLayout(main_signup_layout);
_main_stacked_widget = new QStackedWidget;
_main_stacked_widget->addWidget(login_container);
_main_stacked_widget->addWidget(signup_container);
QVBoxLayout *main_layout = new QVBoxLayout;
main_layout->addWidget(_main_stacked_widget);
this->setLayout(main_layout);
}
LoginDialog::~LoginDialog()
{
delete ui;
}
void LoginDialog::change_page()
{
if(_main_stacked_widget->currentIndex() == 0)
{
_main_stacked_widget->setCurrentIndex(1);
this->setWindowTitle("Registro");
}else
{
_main_stacked_widget->setCurrentIndex(0);
this->setWindowTitle("Log In");
}
}
void LoginDialog::forgot_password(const QString &link)
{
//Q_UNUSED(link);
qDebug() << "Forgot password";
}
void LoginDialog::validate_login()
{
qDebug()<< "Show this line in log file";
std::cout << "Print this line in console\n";
}
void LoginDialog::validate_signup()
{
std::cout << "Trty to create user";
}
login_dialog.h
#ifndef LOGIN_DIALOG_H
#define LOGIN_DIALOG_H
#include <QDialog>
#include <QStackedWidget>
#include <QFormLayout>
#include <QLineEdit>
namespace Ui {
class LoginDialog;
}
class LoginDialog : public QDialog
{
Q_OBJECT
public:
explicit LoginDialog(QWidget *parent = 0);
~LoginDialog();
private slots:
void change_page();
void forgot_password(const QString & link);
void validate_login();
void validate_signup();
private:
Ui::LoginDialog *ui;
//Login Layout
QFormLayout *_login_form_layout;
QLineEdit *_login_email_edit, *_login_password_edit;
//SignUp Layout
QFormLayout *_signup_form_layout;
QLineEdit *_signup_email_edit, *_signup_password_edit;
//Main Layout
QStackedWidget *_main_stacked_widget;
};
#endif // LOGIN_DIALOG_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "login_dialog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void show_login();
private:
Ui::MainWindow *ui;
LoginDialog *_login_dialog;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
_login_dialog = new LoginDialog(this);
}
MainWindow::~MainWindow()
{
delete ui;
delete _login_dialog;
}
void MainWindow::show_login()
{
_login_dialog->show();
}
因此,在主要功能qDebug()<<“启动应用程序”中运行良好,并将该消息写入日志。当我单击登录按钮时。 qdebug消息丢失,但是带有stdout的消息被打印在控制台中。