【C/C++学院】(18)QT文件读写/主窗口类/获取host信息
2023-09-14 08:57:16 时间
QMainWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu
bar)、及一个中心部件(central widget), 是许多应用程序的基础,如文本编辑器。
新建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类用户获得主机信息。
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); }
C/C++ Qt 编译打包项目 Qt程序编译后,需要去qt目录拷贝几个文件,与qt程序放在一起该程序才可以脱离开发环境而独立运行下去,在开发环境下编译好代码以后,还需要进行以下操作将其打包才可以在别的机器上正常运行。
C/C++ Qt 常用数据结构 QT是一个跨平台的图形化类库,常用数据结构就是对C++ STL的二次封装,使其更加易用,如下是经常会用到的一些数据结构和算法笔记。
C/C++ Qt 信号自定义槽函数 Qt中实现自定义信号与槽函数,信号用于发送并触发槽函数,槽函数则是具体的功能实现,如下我们以老师学生为例子简单学习一下信号与槽函数的使用方法。
C/C++ Qt 基本文件读写方法 Qt文件操作有两种方式,第一种使用QFile类的IODevice读写功能直接读写,第二种是利用 QFile和QTextStream结合起来,用流的方式进行文件读写。
C/C++ Qt QThread 线程组件应用 QThread库是QT中提供的跨平台多线程实现方案,使用时需要继承QThread这个基类,并重写实现内部的Run方法,由于该库是基本库,默认依赖于`QtCore.dll`这个基础模块,在使用时无需引入其他模块.
C/C++ Qt QChart 绘图组件应用 QtCharts 组件是QT中提供图表绘制的模块,该模块可以方便的绘制常规图形,Qtcharts 组件基于`GraphicsView`模式实现,其核心是`QChartView`和`QChart`的二次封装版。
C/C++ Qt 基础通用组件的应用 QT 是一个跨平台C++图形界面开发库,利用QT可以快速开发跨平台窗体应用程序,在QT中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率。
C/C++ Qt ToolBar 菜单栏组件应用 ToolBar工具栏在所有窗体应用程序中都广泛被使用,使用ToolBar可以很好的规范菜单功能分类,用户可根据菜单栏来选择不同的功能,Qt中默认自带ToolBar组件,当我们以默认方式创建窗体时,ToolBar就被加入到了窗体中,一般是以QToolBar的方式存在于对象菜单栏,如下所示。
相关文章
- qt通过OpenGL实现3d游戏开发框架
- C++QT常见面试题[通俗易懂]
- c++ auto类型_auto C++
- 一文入门Qt Quick
- qlineedit_qt layoutstretch
- C/C++ Qt 标准Dialog对话框组件应用
- C/C++ Qt QChart 绘图组件应用
- C/C++ Qt 基本文件读写方法
- Qt官方示例-数据库浏览器
- Qt官方示例-Qml系统对话框
- Qt Qml 开发回放监控视频客户端
- Linux串口应用开发实践——Qt篇(linux串口qt)
- Qt Model/View(模型/视图)结构(无师自通)
- 使用Qt框架轻松连接MySQL数据库(qt与mysql连接)
- Linux环境下Qt交叉编译实战(linux交叉编译qt)
- 在 Linux 上快速搭建 Qt 运行环境(qt运行环境linux)
- 简明易懂的Linux下QT安装指南(linux下安装qt教程)
- Qt与SQL Server的数据交互技术(qt sqlserver)
- 使用Linux系统成功运行QT程序的步骤与技巧(linux运行qt程序)
- 从 Qt 中编译到 MySQL 的进程(qt mysql 编译)
- 编辑Linux下使用Qt快速编辑文件(linux下 qt 文件)