QT三种窗口、调试终端信息打印、新建菜单、设置窗口标题名称、界面初始化、打开文件对话框、保存文件对话框

版权声明:墨痕诉清风 https://blog.csdn.net/u012206617/article/details/88396983

三种窗口

QMainWindow:主窗口程序(创建菜单)

QWidget:部件窗口

QDialog:对话框窗口

调试终端信息打印

#include <QtDebug>

qDebug << "not modifed";

新建菜单

快捷键(Alt+F):文件(&F)

扫描二维码关注公众号,回复: 5510987 查看本文章
#include <QtDebug>

private slots:
    void newFileSlot();

QObject::connect(ui->newAction, SIGNAL(triggred(), this, SLOT(newFileSlot()))

void MainWindow::newFileSlot()
{
    //如果当前文档内容已经改变了
    if(ui->textEdit->document()->isModified())
    {
        qDebug() << "current file modifed";
    }
    else
    {
        qDebug << "not modifed";
    }
}

设置窗口标题名称

this->setWindowTitle("1111111111");

界面初始化

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("1111111111");  //初始化位置
    QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(calSlot()));
}

打开文件对话框,读取文件信息

#include <QFile>
#include <QFileDialog>
#include <QDir>

void MainWindow::OpenFileSlot()
{
    //获取程序当前路径
    QString str = QFileDialog::getOpenFileName(this, "Open File", QDir::currentPath());
    if (str.isEmpty())
    {
        QMessageBox
        return;
    }

    QFile *file = new QFile;
    file->setFileName(fileName);
    bool ok = file->open(QIODevice::ReadOnly);
    
    if (ok)
    {
        QTextStream in(file);
        //in.readAll(); //read all
        ui->textEdit->setText(in.readAll());
        file->close();
        delete file;
    }
    else
    {
        return;
    }
}

保存文件对话框

void MainWindow::saveFileSlot()
{
    QString fileName = QFileDialog::getSaveFileName(this, "Save File", QDir::currentPath());
    if (fileName.isE,pty())
    {
        QMessageBOX::information()
        return;
    }

    QFile * file = new QFile;
    file->setFileName(fileName);
    bool ok = file->open(QIODevice::WriteOnly);
    
    if(ok)
    {
        QTextStream out(file);
        out << ui->textEdit->toPlainText();  //取出textEdit纯文本    
        file.close();
        delete file;
    }
    else
    {
        return;
    }
}

猜你喜欢

转载自blog.csdn.net/u012206617/article/details/88396983
今日推荐