简体   繁体   English

qt c++中如何更新QWindow,文字不显示

[英]How to update QWindow in qt c++, text doesn't show

I'm writing QT deskop app and I have a small issue.我正在编写 QT 桌面应用程序,但遇到一个小问题。 I want to open new tab with text that I put from a file.我想用我从文件中输入的文本打开新标签。

I have two class one in mainwindow.h and second in form.h我有两个 class,一个在 mainwindow.h 中,第二个在 form.h 中

mainwindow.h主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

public slots:
    void on_open_file_clicked();

    void on_search_keyword_clicked();

    void on_search_tag_clicked();

    void on_tabWidget_tabCloseRequested(int index);

    void show_tab(QString keywords);
    QString return_text();
public:
    Ui::MainWindow *ui;

private slots:
    void on_actionOpen_file_triggered();
    void on_actionShow_text_file_triggered();
    QVector<QString>find_logs_keywords(QString keywords);
private:
    QString  text = "example text";
};
#endif // MAINWINDOW_H

form.h表格.h

    #ifndef FORM_H
#define FORM_H

#include <QWidget>

namespace Ui {
class Form;
}

class Form : public QWidget
{
    Q_OBJECT

public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();

public slots:
    void on_pushButton_2_clicked();
    void text_to_plain(QString& text);

private:
    Ui::Form *ui;
};

#endif // FORM_H

mainwindow.cpp主窗口.cpp

    #include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QDir>
#include <QVector>
#include <QStringList>
#include <iostream>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "search_keyword.h"
#include "search_tag.h"
#include "form.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_open_file_clicked()
{
    QString filter = "log file (*log) ;; Tex File (*.txt)";
    QString file_name = QFileDialog::getOpenFileName(this, "Open a file", QDir::homePath(), filter);
    QMessageBox::information(this,"..",file_name);
    QFile file(file_name);


    if (!file.open(QFile::ReadOnly | QFile::Text)) QMessageBox::warning(this,"title","file not open");
    else
    {
    QTextStream in(&file);
    text = in.readAll();
    ui->plainTextEdit->setPlainText(text);
    ui->lineEdit->setPlaceholderText(file_name);
    }

    file.close();

}

void MainWindow::on_actionShow_text_file_triggered()
{
    show_tab("all logs");
    Form Fr;
    Fr.text_to_plain(text);
}

void MainWindow::show_tab(QString keywords)
{
    ui->tabWidget->addTab(new Form(), QString("%1").arg(keywords));
    ui->tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
}

form.cpp表格.cpp

#include "form.h"
#include "ui_form.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
}

Form::~Form()
{
    delete ui;
}

void Form::text_to_plain(QString& text)
{
    ui->plainTextEdit->setPlainText(text);
}

My problem is after call method void MainWindow::on_actionShow_text_file_triggered() plain text in QWidget in Form is still empty i tried: this->update, this->repaint, Form::update();我的问题是在调用方法 void MainWindow::on_actionShow_text_file_triggered() 之后,QWidget 中的纯文本仍然是空的我试过:this->update, this->repaint, Form::update(); Form::repaint(), QWidget::repaint();表单::重绘(), QWidget::重绘(); QWidget::update();. QWidget::更新();。 Everything fail.一切都失败了。

the problem is that your Form exists only in this function scope.问题是你的Form只存在于这个function scope中。

void MainWindow::on_actionShow_text_file_triggered()
{
    show_tab("all logs");
    Form Fr;
    Fr.text_to_plain(text);
}

Here -> Form Fr;这里 -> Form Fr; the Form is created: constructor( explicit Form(QWidget *parent = nullptr); ) of the class Form is called创建表单:调用 class 表单的构造函数( explicit Form(QWidget *parent = nullptr);

And at this place -> } your Form is deleted: destructor( ~Form(); ) of class Form is called.在这个地方 -> }你的表格被删除:调用 class 表格的析构函数( ~Form(); )。 This happens because you create your Form instance on the function stack.发生这种情况是因为您在 function 堆栈上创建了 Form 实例。 So Fr is local object and exist only in the scope of the function on_actionShow_text_file_triggered() after the function is executed/finished all local variables are freed/destructed.所以Fr是本地的object,只存在于function on_actionShow_text_file_triggered()的scope中,在function执行/完成后,所有局部变量都被释放/销毁。

if your Fr object should exist not just in this function you need to create it on the heap instead of stack.如果你的Fr object 不应该只存在于这个 function 中,你需要在堆上而不是堆栈上创建它。 PS: for case study read about stack vs heap for example hear: What and where are the stack and heap? PS:对于案例研究,例如阅读有关堆栈与堆的内容:堆栈和堆是什么以及在哪里?

Your on_actionShow_text_file_triggered function should then look like this:您的on_actionShow_text_file_triggered function 应该如下所示:

void MainWindow::on_actionShow_text_file_triggered()
{
    show_tab("all logs");
    Form * Fr = new Form(this); //if you set parent object your Form will be automatically deleted/destructed then MainWindow is deleted/destructed
    Fr->text_to_plain(text);
    Fr->show(); // you should call show() function of QWidget class and subclasses to get your widget visible
}

This Form * Fr = new Form(this);这个Form * Fr = new Form(this); will create new form pointer every time.每次都会创建新的表单指针。 Would not it be better to create one global pointer to the form, call the functions and delete it later?创建一个指向表单的全局指针,调用函数然后删除它不是更好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM