开发者社区> 吴英强> 正文

【C/C++学院】(18)QT文件读写/主窗口类/获取host信息

简介: <h1><span style="font-family:'Microsoft YaHei UI','Microsoft YaHei',SimSun,'Segoe UI',Tahoma,Helvetica,sans-serif,'Microsoft YaHei',Georgia,Helvetica,Arial,sans-serif,宋体,PMingLiU,serif; font-size:
+关注继续查看

1.文件读写

QT提供了QFile类用于文件读写。
QFile可以读写文本文件,也可以读写二进制文件
#include <QFile>
#include <QTextStream>

读文本文件例子

QString s;
QFile file("abc.txt);
if (file.open(QFile::ReadOnly))
{
    QTextStream stream(&file);
    while (!stream.atEnd())
    {
        s = stream.readLine();
        QMessageBox::information(this, "文件内容", s);  
    }
    file.close();
}

void Widget::on_clicked()
{
    QString filename = QFileDialog::getOpenFileName();
    QFile file(filename);
    QString s;
    if (file.open(QFile::ReadOnly))
    {
        QTextStream stream(&file);
        s = stream.readAll();
        QMessageBox::information(this, "文件内容", s);  
        file.close();
    }
}

写文本文件例子。

windows: 换行"\r\n"
linux:换行"\n"
void Widget::on_clicked()
{
    QFile file("abc.txt");
    if (file.open(QFile::WriteOnly | QFile::Truncate))
    {
        QTextStream out (&file);
        out << tr("hello, world\n");
        out << tr("new line \r\n");
        out << tr("abcde\r\n");
    }
}


2.主窗口类

QMainWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu bar)、及一个中心部件(central widget), 是许多应用程序的基础,如文本编辑器。


QMainWindow中菜单需要QMenu类和QAction类来实现。
QAction类定义了菜单的具体行为。
QMainWindow中提供了menuBar()函数返回一个menuBar。
通过调用menuBar的addMenu函数就可以生成一个新的菜单项。
QMenu类addAction函数为菜单指定一个QAction。
QMainWindow中提供了自己的布局控件,所以不需要再为QMainWindow定义布局控件。

新建Qt应用,基类选择“QMainWindow”,取消“创建界面”复选框的选中状态。

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QAction>
#include <QTextEdit>
#include <QMenuBar>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void openfile();
    void exitfile();
private:
    QAction *open, *exit;
    QMenu *menu;
    QTextEdit *edit1;
};

#endif // MAINWINDOW_H

#include "mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    open = new QAction(tr("打开"), this);//建立一个Action
    open->setShortcut(tr("Ctrl + o"));//设置快捷方式

    exit = new QAction(tr("退出"), this);
    exit->setShortcut(tr("ctrl + e"));
    QMenuBar * menubar = menuBar();//调用MainWindow的menuBar方法,得到一个menubar
    menu = menubar->addMenu(tr("文件"));//加入一个新的菜单项
    menu->addAction(open);//建立一个Action
    menu->addAction(exit);

    edit1 = new QTextEdit;
    setCentralWidget(edit1);
    connect(open, SIGNAL(triggered()),this, SLOT(openfile()));
    connect(exit, SIGNAL(triggered()), this, SLOT(exitfile()));
}

MainWindow::~MainWindow()
{

}

void MainWindow::openfile()
{
    QFileDialog::getOpenFileName();
}

void MainWindow::exitfile()
{
    close();
}


3.获取host信息

QT如果要进行网络编程首先需要在.pro文件中添加如下代码:
QT += network
在头文件中包含相关头文件:
#include <QHostInfo>
#include <QNetworkInterface>
QHostInfo类用户获得主机信息。
QNetworkInterface类获得与网络接口相关的信息。
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QPushButton>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();
private slots:
    void on_click();
private:
    QPushButton *btn1;
};

#endif // WIDGET_H

#include "widget.h"
#include <QMessageBox>
#include <QHBoxLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    btn1 = new QPushButton;
    btn1->setText("获取host信息");
    QHBoxLayout *layout1 = new QHBoxLayout(this);
    layout1->addWidget(btn1);
    connect(btn1, SIGNAL(clicked()), this, SLOT(on_click()));
}

Widget::~Widget()
{

}

void Widget::on_click()
{
    QString s = QHostInfo::localHostName();
    QMessageBox::information(this, "标题", s);
}

void Widget::on_click()
{
    QString s = QHostInfo::localHostName();
    QHostInfo info = QHostInfo::fromName(s);
    QList<QHostAddress> list = info.addresses();//得到主机所有的网络地址
    if (!list.isEmpty())
    {
        QList<QHostAddress>::iterator i;//设置一个迭代器
        for (i=list.begin(); i!=list.end(); i++)//遍历list
        {
            QMessageBox::information(this, tr("提示"), (*i).toString());
        }
    }
}

void Widget::on_click()
{
    QString detail = "";
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();//得到本机所有的网络接口
    QList<QNetworkInterface>::iterator i;//声明一个迭代器
    for (i=list.begin(); i!=list.end(); i++)
    {
        QNetworkInterface interface = *i;//声明一个QNetworkInterface等于迭代器的值
        detail = tr("设备")+interface.name()+"\n";//得到设备名称
        detail = detail+tr("硬件地址:")+interface.hardwareAddress()+"\n";//得到设备地址
        QList<QNetworkAddressEntry> entryList = interface.addressEntries();
        QList<QNetworkAddressEntry>::iterator j;
        for(j=entryList.begin(); j!=entryList.end(); j++)
        {
            QNetworkAddressEntry entry = *j;
            detail = detail+"\t"+tr("ip 地址:")+entry.ip().toString()+"\n";
            detail = detail+"\t"+tr("子网掩码:")+entry.netmask().toString()+"\n";
            detail = detail+"\t"+tr("广播地址:")+entry.broadcast().toString()+"\n";
        }
        QMessageBox::information(this, tr("Detail"), detail);
    }
}





版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Qt+ECharts开发笔记(二):Qt窗口动态调整大小,使ECharts跟随Qt窗口大小变换而变换大小
上一篇将ECharts嵌入Qt中,在开始ECharts使用之前,还有一个很重要的功能,就是在窗口变换大小的时候,ECharts的图表尺寸也要跟随Qt窗口变换大小而变换大小。
131 0
Qt实用技巧:将QWidget作为输入窗口,接收键盘消息、输入法并且控制输入法悬浮工具栏位置控制
Qt实用技巧:将QWidget作为输入窗口,接收键盘消息、输入法并且控制输入法悬浮工具栏位置控制
429 0
Qt实用技巧:Qt窗口置顶
Qt实用技巧:Qt窗口置顶
297 0
关于 Qt场景QGraphicsScene中,添加QWidget窗口,QWidget窗口无法捕捉mouseReleaseEvent松开消息 的解决方法
关于 Qt场景QGraphicsScene中,添加QWidget窗口,QWidget窗口无法捕捉mouseReleaseEvent松开消息 的解决方法
258 0
关于 Qt设置置顶窗口,透明部分显示黑色底色(已设置透明窗口) 的解决方法
关于 Qt设置置顶窗口,透明部分显示黑色底色(已设置透明窗口) 的解决方法
394 0
Qt实用技巧:实现不规则窗口的鼠标消息穿透,包括穿透到桌面和穿透到父窗口
Qt实用技巧:实现不规则窗口的鼠标消息穿透,包括穿透到桌面和穿透到父窗口
275 0
Qt实用技巧:自定义窗口标题栏
Qt实用技巧:自定义窗口标题栏
349 0
Qt实用技巧:实现窗口透明的五种方法
Qt实用技巧:实现窗口透明的五种方法
2058 0
Qt实用技巧:在Qt Gui程序中嵌入qml界面(可动态覆盖整个窗口)
Qt实用技巧:在Qt Gui程序中嵌入qml界面(可动态覆盖整个窗口)
478 0
Qt窗口关闭和应用程序停止是否调用析构函数的一些说明
Qt窗口关闭和应用程序停止是否调用析构函数的一些说明
245 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
继承与功能组合
立即下载
对象的生命期管理
立即下载
移动与复制
立即下载