30.QT IDE编写
- mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QTextEdit>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QFileDialog>
#include <QMessageBox>
#include <iostream>
#include <fstream>
#include <cstdlib> #include <QImage>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QComboBox>
#include <QSpinBox>
#include <QToolBar>
#include <QFontComboBox>
#include <QToolButton>
#include <QTextCharFormat>
using namespace std; namespace Ui {
class MainWindow;
} class MainWindow : public QMainWindow
{
Q_OBJECT public:
explicit MainWindow(QWidget *parent = );
~MainWindow();
void createActions();
void createMenus();
void createToolBars(); private:
Ui::MainWindow *ui;
QString filename;
//文本编辑
QTextEdit *text;
//菜单
QMenu *file;
//菜单选项
QAction *Afile_open;
QAction *Afile_close;
QAction *Afile_save;
QAction *Afile_exit; QMenu *edit;
QAction *Afile_selall;
QAction *Afile_cut;
QAction *Afile_copy;
QAction *Afile_paste; QMenu *build;
QAction *Abuild_it;
QAction *Abuild_andrun; QMenu *about;
QAction *Aabout_it; private:
QMenu *fileMenu; //各项菜单栏
QMenu *zoomMenu;
QMenu *rotateMenu;
QMenu *mirrorMenu; QImage img;
QString fileName;
//编辑显示窗口
// ShowWidget *showWidget; QAction *openFileAction; //文件菜单项
QAction *NewFileAction;
QAction *PrintTextAction;
QAction *PrintImageAction;
QAction *exitAction; QAction *copyAction; //编辑菜单项
QAction *cutAction;
QAction *pasteAction;
QAction *aboutAction;
QAction *zoomInAction;
QAction *zoomOutAction; QAction *rotate90Action; //旋转菜单项
QAction *rotate180Action;
QAction *rotate270Action; QAction *mirrorVerticalAction; //镜像菜单项
QAction *mirrorHorizontalAction; QAction *undoAction;
QAction *redoAction; QToolBar *fileTool; //工具栏
QToolBar *zoomTool;
QToolBar *rotateTool;
QToolBar *mirrorTool; QToolBar *doToolBar; QLabel *fontLabel1; //字体设置项
QFontComboBox *fontComboBox;
QLabel *fontLabel2;
QComboBox *sizeComboBox;
QToolButton *boldBtn;
QToolButton *italicBtn;
QToolButton *underlineBtn;
QToolButton *colorBtn; QToolBar *fontToolBar; //字体工具栏 QLabel *listLabel; //排序设置项
QComboBox *listComboBox;
QActionGroup *actGrp;
QAction *leftAction;
QAction *rightAction;
QAction *centerAction;
QAction *justifyAction; QToolBar *listToolBar; //排序工具栏
private slots:
void file_open();
void file_close();
void file_save();
void file_exit();
void file_selall();
void file_cut();
void file_copy();
void file_paste();
void build_it();
void build_andrun();
void about_it(); }; #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);
//设置文本编辑器
text = new QTextEdit;
QFont font;
font.setPixelSize();
text->setFont(font);
QColor qcolor;
qcolor.setRgb(,,);
this->setCentralWidget(text); //设置菜单
file = this->menuBar()->addMenu("文件");
edit = this->menuBar()->addMenu("编辑");
build = this->menuBar()->addMenu("构建");
about = this->menuBar()->addMenu("关于"); //about菜单
{
Aabout_it = new QAction("关于",this);
about->addAction(Aabout_it);
connect(Aabout_it,SIGNAL(triggered(bool)),this,SLOT(about_it()));
} //open菜单
{
Afile_open = new QAction("打开文件",this);
file->addAction(Afile_open);
connect(Afile_open,SIGNAL(triggered(bool)),this,SLOT(file_open())); Afile_close = new QAction("关闭文件",this);
file->addAction(Afile_close);
connect(Afile_close,SIGNAL(triggered(bool)),this,SLOT(file_close())); Afile_save = new QAction("保存文件",this);
file->addAction(Afile_save);
connect( Afile_save,SIGNAL(triggered(bool)),this,SLOT(file_save())); Afile_exit = new QAction("退出",this);
file->addAction(Afile_exit);
connect(Afile_exit,SIGNAL(triggered(bool)),this,SLOT(file_exit()));
} //编辑菜单
{
Afile_selall = new QAction("全选",this);
edit->addAction(Afile_selall);
connect(Afile_selall,SIGNAL(triggered(bool)),this,SLOT(file_selall())); Afile_cut = new QAction("剪切",this);
edit->addAction(Afile_cut);
connect(Afile_cut,SIGNAL(triggered(bool)),this,SLOT(file_cut())); Afile_copy = new QAction("复制",this);
edit->addAction(Afile_copy);
connect(Afile_copy,SIGNAL(triggered(bool)),this,SLOT(file_copy())); Afile_paste = new QAction("粘贴",this);
edit->addAction(Afile_paste);
connect(Afile_paste,SIGNAL(triggered(bool)),this,SLOT(file_paste()));
} {
Abuild_it = new QAction("编译",this);
build->addAction(Abuild_it);
connect(Abuild_it,SIGNAL(triggered(bool)),this,SLOT(build_it())); Abuild_andrun = new QAction("编译执行",this);
build->addAction(Abuild_andrun);
connect(Abuild_andrun,SIGNAL(triggered(bool)),this,SLOT(build_andrun()));
} this->setWindowTitle("简易编译器"); //showWidget =new ShowWidget(this);
// setCentralWidget(showWidget);
//在工具栏上嵌入控件
//设置字体
fontLabel1 =new QLabel(tr("字体:"));
fontComboBox =new QFontComboBox;
fontComboBox->setFontFilters(QFontComboBox::ScalableFonts); fontLabel2 =new QLabel(tr("字号:"));
sizeComboBox =new QComboBox;
QFontDatabase db;
foreach(int size,db.standardSizes())
sizeComboBox->addItem(QString::number(size)); boldBtn =new QToolButton;
boldBtn->setIcon(QIcon("bold.png"));
boldBtn->setCheckable(true);
italicBtn =new QToolButton;
italicBtn->setIcon(QIcon("italic.png"));
italicBtn->setCheckable(true); underlineBtn =new QToolButton;
underlineBtn->setIcon(QIcon("underline.png"));
underlineBtn->setCheckable(true); colorBtn =new QToolButton;
colorBtn->setIcon(QIcon("color.png"));
colorBtn->setCheckable(true); //排序
listLabel =new QLabel(tr("排序"));
listComboBox =new QComboBox;
listComboBox->addItem("Standard");
listComboBox->addItem("QTextListFormat::ListDisc");
listComboBox->addItem("QTextListFormat::ListCircle");
listComboBox->addItem("QTextListFormat::ListSquare");
listComboBox->addItem("QTextListFormat::ListDecimal");
listComboBox->addItem("QTextListFormat::ListLowerAlpha");
listComboBox->addItem("QTextListFormat::ListUpperAlpha");
listComboBox->addItem("QTextListFormat::ListLowerRoman");
listComboBox->addItem("QTextListFormat::ListUpperRoman"); createActions();
createMenus();
createToolBars(); if(img.load("image.png"))
{
//showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
} } MainWindow::~MainWindow()
{
delete ui;
} void MainWindow::file_open()
{
filename =QFileDialog::getOpenFileName(this,"hello","*.*");
if(filename.isEmpty())
{
QMessageBox::information(this,"提示","注意选择一个文件");
return;
}
fstream fin(filename.toStdString().c_str());
if(!fin)
{
QMessageBox::information(this,"提示","打开文件失败");
return;
} while(!fin.eof())
{
char buf[]={};
fin.getline(buf,);
text->append(buf);
}
fin.close();
} void MainWindow::file_close()
{
text->setText("");
filename ="";
} void MainWindow::file_save()
{
filename = QFileDialog::getSaveFileName();
if(filename.isEmpty())
{
QMessageBox::information(this,"提示","打开文件失败");
return;
} QString info =text->toPlainText().toStdString().c_str();
//写入
ofstream fout(filename.toStdString().c_str());
if(!fout)
{
QMessageBox::information(this,"提示","打开文件失败");
return;
}
fout.write(info.toStdString().c_str(),strlen(info.toStdString().c_str()));
fout.close();
} void MainWindow::file_exit()
{
exit();
} void MainWindow::file_selall()
{
text->selectAll();
} void MainWindow::file_cut()
{
text->cut();
} void MainWindow::file_copy()
{
text->copy();
} void MainWindow::file_paste()
{
text->paste();
} void MainWindow::build_it()
{
QString destexename = filename;
bool iscpp = destexename.contains(".cpp");
QString cmd;
if(iscpp)
{
destexename.replace(".cpp",".exe");
cmd = "g++ -o " + destexename+" " +filename;
}
else
{
destexename.replace(".c",".exe");
cmd = "gcc -o "+destexename+" "+filename;
}
system(cmd.toStdString().c_str());
} void MainWindow::build_andrun()
{
QString destexename = filename;
bool iscpp = destexename.contains(".cpp");
QString cmd;
if(iscpp)
{
destexename.replace(".cpp",".exe");
cmd = "g++ -o "+filename+" "+destexename;
}
else
{
destexename.replace(".c",".exe");
cmd = "gcc -o "+filename+" "+destexename;
}
system(cmd.toStdString().c_str()); system(destexename.toStdString().c_str());
} void MainWindow::about_it()
{
QMessageBox::information(this,"hello","版权所有,盗版不究");
} void MainWindow::createActions()
{
//"打开"动作
openFileAction =new QAction(QIcon("open.png"),tr("打开"),this);
openFileAction->setShortcut(tr("Ctrl+O"));
openFileAction->setStatusTip(tr("打开一个文件"));
// connect(openFileAction,SIGNAL(triggered()),this,SLOT(ShowOpenFile())); //"新建"动作
NewFileAction =new QAction(QIcon("new.png"),tr("新建"),this);
NewFileAction->setShortcut(tr("Ctrl+N"));
NewFileAction->setStatusTip(tr("新建一个文件"));
//connect(NewFileAction,SIGNAL(triggered()),this,SLOT(ShowNewFile())); //"退出"动作
exitAction =new QAction(tr("退出"),this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("退出程序"));
//connect(exitAction,SIGNAL(triggered()),this,SLOT(close())); //"复制"动作
copyAction =new QAction(QIcon("copy.png"),tr("复制"),this);
copyAction->setShortcut(tr("Ctrl+C"));
copyAction->setStatusTip(tr("复制文件"));
// connect(copyAction,SIGNAL(triggered()),showWidget->text,SLOT(copy())); //"剪切"动作
cutAction =new QAction(QIcon("cut.png"),tr("剪切"),this);
cutAction->setShortcut(tr("Ctrl+X"));
cutAction->setStatusTip(tr("剪切文件"));
//connect(cutAction,SIGNAL(triggered()),showWidget->text,SLOT(cut())); //"粘贴"动作
pasteAction =new QAction(QIcon("paste.png"),tr("粘贴"),this);
pasteAction->setShortcut(tr("Ctrl+V"));
pasteAction->setStatusTip(tr("粘贴文件"));
// connect(pasteAction,SIGNAL(triggered()),showWidget->text,SLOT(paste())); //"关于"动作
aboutAction =new QAction(tr("关于"),this);
// connect(aboutAction,SIGNAL(triggered()),this,SLOT(QApplication::aboutQt())); //"打印文本"动作
PrintTextAction =new QAction(QIcon("printText.png"),tr("打印文本"), this);
PrintTextAction->setStatusTip(tr("打印一个文本"));
// connect(PrintTextAction,SIGNAL(triggered()),this,SLOT(ShowPrintText())); //"打印图像"动作
PrintImageAction =new QAction(QIcon("printImage.png"),tr("打印图像"), this);
PrintImageAction->setStatusTip(tr("打印一幅图像"));
// connect(PrintImageAction,SIGNAL(triggered()),this,SLOT(ShowPrintImage())); //"放大"动作
zoomInAction =new QAction(QIcon("zoomin.png"),tr("放大"),this);
zoomInAction->setStatusTip(tr("放大一张图片"));
// connect(zoomInAction,SIGNAL(triggered()),this,SLOT(ShowZoomIn())); //"缩小"动作
zoomOutAction =new QAction(QIcon("zoomout.png"),tr("缩小"),this);
zoomOutAction->setStatusTip(tr("缩小一张图片"));
// connect(zoomOutAction,SIGNAL(triggered()),this,SLOT(ShowZoomOut())); //实现图像旋转的动作(Action)
//旋转90°
rotate90Action =new QAction(QIcon("rotate90.png"),tr("旋转90°"),this);
rotate90Action->setStatusTip(tr("将一幅图旋转90°"));
// connect(rotate90Action,SIGNAL(triggered()),this,SLOT(ShowRotate90())); //旋转180°
rotate180Action =new QAction(QIcon("rotate180.png"),tr("旋转180°"), this);
rotate180Action->setStatusTip(tr("将一幅图旋转180°"));
connect(rotate180Action,SIGNAL(triggered()),this,SLOT(ShowRotate180())); //旋转270°
rotate270Action =new QAction(QIcon("rotate270.png"),tr("旋转270°"), this);
rotate270Action->setStatusTip(tr("将一幅图旋转270°"));
// connect(rotate270Action,SIGNAL(triggered()),this,SLOT(ShowRotate270())); //实现图像镜像的动作(Action)
//纵向镜像
mirrorVerticalAction =new QAction(tr ("纵向镜像"),this);
mirrorVerticalAction->setStatusTip(tr("对一张图作纵向镜像"));
// connect(mirrorVerticalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorVertical())); //横向镜像
mirrorHorizontalAction =new QAction(tr("横向镜像"),this);
mirrorHorizontalAction->setStatusTip(tr("对一张图作横向镜像"));
//connect(mirrorHorizontalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorHorizontal())); //排序:左对齐、右对齐、居中和两端对齐
actGrp =new QActionGroup(this); leftAction =new QAction(QIcon("left.png"),"左对齐",actGrp);
leftAction->setCheckable(true); rightAction =new QAction(QIcon("right.png"),"右对齐",actGrp);
rightAction->setCheckable(true); centerAction =new QAction(QIcon("center.png"),"居中",actGrp);
centerAction->setCheckable(true); justifyAction =new QAction(QIcon("justify.png"),"两端对齐",actGrp);
justifyAction->setCheckable(true); // connect(actGrp,SIGNAL(triggered(QAction*)),this,SLOT(ShowAlignment(QAction*))); //实现撤销和重做的动作(Action)
//撤销和重做
undoAction =new QAction(QIcon("undo.png"),"撤销",this);
// connect(undoAction,SIGNAL(triggered()),showWidget->text,SLOT(undo()));
redoAction =new QAction(QIcon("redo.png"),"重做",this);
//connect(redoAction,SIGNAL(triggered()),showWidget->text,SLOT(redo()));
} void MainWindow::createMenus()
{
//文件菜单
fileMenu =menuBar()->addMenu(tr("文件"));
fileMenu->addAction(openFileAction);
fileMenu->addAction(NewFileAction);
fileMenu->addAction(PrintTextAction);
fileMenu->addAction(PrintImageAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction); //缩放菜单
zoomMenu =menuBar()->addMenu(tr("编辑"));
zoomMenu->addAction(copyAction);
zoomMenu->addAction(cutAction);
zoomMenu->addAction(pasteAction);
zoomMenu->addAction(aboutAction);
zoomMenu->addSeparator();
zoomMenu->addAction(zoomInAction);
zoomMenu->addAction(zoomOutAction); //旋转菜单
rotateMenu =menuBar()->addMenu(tr("旋转"));
rotateMenu->addAction(rotate90Action);
rotateMenu->addAction(rotate180Action);
rotateMenu->addAction(rotate270Action); //镜像菜单
mirrorMenu =menuBar()->addMenu(tr("镜像"));
mirrorMenu->addAction(mirrorVerticalAction);
mirrorMenu->addAction(mirrorHorizontalAction);
} void MainWindow::createToolBars()
{
//文件工具条
fileTool =addToolBar("File");
fileTool->addAction(openFileAction);
fileTool->addAction(NewFileAction);
fileTool->addAction(PrintTextAction);
fileTool->addAction(PrintImageAction); //编辑工具条
zoomTool =addToolBar("Edit");
zoomTool->addAction(copyAction);
zoomTool->addAction(cutAction);
zoomTool->addAction(pasteAction);
zoomTool->addSeparator();
zoomTool->addAction(zoomInAction);
zoomTool->addAction(zoomOutAction); //旋转工具条
rotateTool =addToolBar("rotate");
rotateTool->addAction(rotate90Action);
rotateTool->addAction(rotate180Action);
rotateTool->addAction(rotate270Action); //撤销和重做工具条
doToolBar =addToolBar("doEdit");
doToolBar->addAction(undoAction);
doToolBar->addAction(redoAction); //字体工具条
fontToolBar =addToolBar("Font");
fontToolBar->addWidget(fontLabel1);
fontToolBar->addWidget(fontComboBox);
fontToolBar->addWidget(fontLabel2);
fontToolBar->addWidget(sizeComboBox);
fontToolBar->addSeparator();
fontToolBar->addWidget(boldBtn);
fontToolBar->addWidget(italicBtn);
fontToolBar->addWidget(underlineBtn);
fontToolBar->addSeparator();
fontToolBar->addWidget(colorBtn); //排序工具条
listToolBar =addToolBar("list");
listToolBar->addWidget(listLabel);
listToolBar->addWidget(listComboBox);
listToolBar->addSeparator();
listToolBar->addActions(actGrp->actions());
} - main.cpp
#include "mainwindow.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.resize(,);
return a.exec();
}
30.QT IDE编写的更多相关文章
- 利用Eric+Qt Designer编写倒计时时钟
[前言]前几日通过编写命令行通讯录,掌握了Python的基本语法结构,于是开始向更高水平冲击,利用Eric与Qt Designer 编写一个带界面的小程序.本次实操中也确实遇到了不少问题,通过学习也都 ...
- 用Sublime Text搭建简易IDE编写Verilog代码
前言 Verilog是一种硬件描述语言(HDL),该语言在Windows上有集成开发环境可以使用,如ModelSim,但ModelSim的编辑器不太好用因此笔者萌生了用Sublime Text3来编写 ...
- 利用IDE编写C语言程序的一点注意事项
前言:我是喜欢编程的一只菜鸟,在自学过程中,对遇到的一些问题和困惑,有时虽有一点体会感悟,但时间一长就会淡忘,很不利于知识的积累.因此,想通过博客园这个平台,一来记录自己的学习体会,二来便于向众多高手 ...
- Qt+QZXing编写识别二维码的程序
本人最近在用Qt编写程序,需要用编写二维码识别功能.在网上搜寻一番,找到了QZXing.配置过程中确实出了一大把汗,这里我写这篇文章记录配置方法,替后人省一把汗吧!我的开发环境:MSVC2010 + ...
- VLC和Qt结合编写流媒体rtsp播放器
VLC播放器是一款功能强大且小巧的播放器,它支持多种多样的音视频格式,比如MPEG1,2以及mp3等等.并且通过Qt和VLC的结合能让每个开发者写出自己的视频流媒体播放器. Q ...
- Qt *.pro编写一般规则
qmake 之 CONFIG 与 QT 乱谈 看qtcn论坛中经常有人忘记 QT+=network 等语句.随便写写吧,或许对他人有帮助. 写来写去,发现越写越乱,就这样吧,反正主要的内容很简单. d ...
- Qt+imx6编写的楼宇对讲管理平台
第一个初步版本. 1:楼宇对讲模块.住户报警模块.门禁控制模块.系统设置模块. 2:实时对讲信息卡片式展示,通话记录表格展示. 3:设备面板展示,实时显示上下线报警等信息. 4:设备查询.记录查询.运 ...
- windows下Qt Creator5.1.0编写程序以及调用OpenCV库
系统说明 最近使用opencv编写程序,程序编的差不多就学习使用QT加个界面,首先声明下本人的系统和使用的软件版本, 系统: windows xp QT IDE:QT Creator5.1.0 Ope ...
- 编写第一个Qt程序
http://c.biancheng.net/view/1817.html 学习一种编程语言或编程环境,通常会先编写一个“Hello World”程序.我们也用 Qt Creator 编写一个“Hel ...
随机推荐
- APM技术原理
链接地址:http://www.infoq.com/cn/articles/apm-Pinpoint-practice 1.什么是APM? APM,全称:Application Performance ...
- [转]SURF算法解析
SURF算法解析 一.积分图像 积分图像的概念是由Viola和Jones提出的.积分图像中任意一点(i,j)的值为原图像左上角到任意点(i,j)相应的对焦区域的灰度值的总和,其数学公式如图1所示 ...
- PythonOpencv-分类器—SVM,KNearest,RTrees,Boost,MLP
原文链接:http://blog.csdn.net/gjy095/article/details/9243153 上一篇文章,不是很详细,这一篇解释的清晰些,请访问原始链接. Rtrees介绍!参考链 ...
- 编写jQuery 插件
编写jQuery Plugin,要设置默认值,并允许用户修改默认值,或者运行是传入其他值. 最终,我们得出编写一个jQuery插件的原则: 给$.fn绑定函数,实现插件的代码逻辑: 插件函数最后要 r ...
- Linux 内核剖解(转)
Linux 内核剖析(转) linux内核是一个庞大而复杂的操作系统的核心,不过尽管庞大,但是却采用子系统和分层的概念很好地进行了组织.在本文中,您将探索 Linux 内核的总体结构,并学习一些主要 ...
- linux中errno使用(转)
当linux中的C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因,在实际编程中用这一招解决了不少 ...
- Ubuntu Server 18.04 LTS 安装
版本:Ubuntu Server 18.04.1 LTS 环境:VMware Workstation 14 Player 下载地址:https://www.ubuntu.com/download/se ...
- python爬虫简单架构原理及示例
网页下载器示例: # coding:utf-8 import urllib2 import cookielib url = "http://www.baidu.com" print ...
- Selenium 安装与配置及webdriver的API与定位元素
1. selenium安装命令行 C:\Users\wu>cd /d E:\soft\python3.6\Scripts E:\soft\python3.6\Scripts>pip3 in ...
- React传递参数的多种方式
最常见的就是父子组件之间传递参数 父组件往子组件传值,直接用this.props就可以实现 在父组件中,给需要传递数据的子组件添加一个自定义属性,在子组件中通过this.props就可以获取到父组件传 ...