一、主窗口
上面就是一个主窗口,主窗口中的每一个都是Action
这次新建工程要选择mainwindow
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAction>
#include <QTextEdit>
#include <QLabel>
#include <QFileDialog>
#include <QColorDialog>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void openf()
{
QString filename = QFileDialog::getOpenFileName();
te->setText(filename);
}
void copyf()
{
te->copy();
}
void pastf()
{
te->paste();
}
void setcf()
{
QColor ccc = QColorDialog::getColor();
te->setTextColor(ccc);
}
private:
QAction *openfileact;
QAction *copyfileact, *pastfileact;
QAction *setcoloract;
//central
QTextEdit *te;
//status
QLabel *slb;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//create actions
openfileact = new QAction(QIcon("open.png"), "open");
openfileact->setShortcut(QKeySequence(tr("Ctrl+O")));
copyfileact = new QAction(QIcon("copy.png"),"copy");
pastfileact = new QAction(QIcon("past.png"),"past");
setcoloract = new QAction(QIcon("color.png"),"setcolor");
//create meu
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openfileact);
QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(copyfileact);
editMenu->addAction(pastfileact);
QMenu *setMenu = menuBar()->addMenu(tr("&Set"));
setMenu->addAction(setcoloract);
//create toolbars
QToolBar *tfile = addToolBar("file");
tfile->addAction(openfileact);
QToolBar *tedit = addToolBar("edit");
tedit->addAction(copyfileact);
tedit->addAction(pastfileact);
QToolBar *tset = addToolBar("set");
tset->addAction(setcoloract);
//cenctral
te = new QTextEdit("hello");
te->setMinimumSize(640, 480);
setCentralWidget(te);
//statures
slb = new QLabel("1.txt*");
QStatusBar *stb = statusBar();
stb->addWidget(slb);
connect(openfileact, SIGNAL(triggered(bool)), this, SLOT(openf()));
connect(copyfileact, SIGNAL(triggered(bool)), this, SLOT(copyf()));
connect(pastfileact, SIGNAL(triggered(bool)), this, SLOT(pastf()));
connect(setcoloract, SIGNAL(triggered(bool)), this, SLOT(setcf()));
}
MainWindow::~MainWindow()
{
}
运行后是这样的,
还可以使用快捷方式并且有小图标。
可以复制黏贴设置颜色,反正需要什么加什么就好了
二、事件
2.1事件处理
定时器一秒输出一次
鼠标点击记录位置
滑轮滚动输出正负,按键输出的是标号不是具体的按下了什么
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include <QMouseEvent>//鼠标
#include <QKeyEvent>//键盘
#include <QWheelEvent>//滚轮
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void mousePressEvent(QMouseEvent *event)
{
qDebug()<<"pressssss: "<<event->pos();
}
void mouseMoveEvent(QMouseEvent *event)
{
qDebug()<<"moving!"<<event->pos();
}
void keyPressEvent(QKeyEvent *event)
{
qDebug()<<"key: "<<event->key();
}
void wheelEvent(QWheelEvent *event)
{
qDebug()<<"wheel: "<<event->delta();
}
void timerEvent(QTimerEvent *event)
{
qDebug()<<"timer!";
}
};
#endif // WIDGET_H
2.2绘图事件
可以像一个表一样是可以动的
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include <QPainter>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void paintEvent(QPaintEvent *event)
{
qDebug()<<"ppppppp";
QPainter p(this);
p.translate(this->width()/2, this->height()/2);
p.drawEllipse(QPoint(0, 0), 100, 100);
// p.drawLine(0, 0, 50, 50);
p.rotate(i++*10);
p.drawLine(0, 0, 50, 50);
}
void timerEvent(QTimerEvent *event)
{
update();
}
private:
int i;
};
#endif // WIDGET_H
2.3双缓冲绘图
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QMouseEvent>
#include <QPainter>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void mousePressEvent(QMouseEvent *event)
{
startp = event->pos();
}
void mouseReleaseEvent(QMouseEvent *event)
{
saveit = true;
update();
}
void mouseMoveEvent(QMouseEvent *event)
{
endp = event->pos();
update();
}
void paintEvent(QPaintEvent *event)
{
if(saveit)
{
QPainter px(pix);
px.drawLine(startp, endp);
saveit = false;
}
QPainter p(this);
p.drawPixmap(0, 0, *pix);
p.drawLine(startp, endp);
}
private:
QPoint startp;//起点
QPoint endp;//终点
QPixmap *pix;
bool saveit;
};
#endif // WIDGET_H
双缓冲绘图其实就是把第一次的绘图用一个东西保存了一下再一起显示出来,保证之前的痕迹可以保留
2.4事件过滤器
过滤器就是对事件进行过滤的一个类,他只要是Qobject类的派生类,并且他满足一个eventFilter
上图的y就是一个过滤器,y不要的x才处理。
下面用一个照片阅读器来演示一下什么是过滤器
没啥看的看截图吧
代码有点多还是放资源里。