1、void QWidget::paintEvent(QPaintEvent * event) [virtual protected]
A paint event is a request to repaint all or part of a widget.
It can happen for one of the following reasons:
1.repaint() orupdate() was invoked(调用)
2.the widget was obscured(遮挡) and has now been uncovered(露出), or many other reasons.
------------------------------------------------------------------------------------------------------
Many widgets can simply repaint their entire surface when asked to, but some slow widgets need to optimize by painting only the requested region/*通过绘制被请求的区域来优化*/:QPaintEvent::region().
------------------------------------------------------------------------------------------------------
Qt also tries to speed up painting by merging multiple paint events into one/*合并多个绘制事件*/. When update() is called several times or the window system sends several paint events, Qt merges these events into one event with a larger region (seeQRegion::united()). Therepaint() function does not permit this optimization, so we suggest usingupdate()whenever possible./*如果有可能,应尽量使用update()*/
------------------------------------------------------------------------------------------------------
When the paint event occurs, the update region has normally been erased, so you are painting on the widget's background./*在背景上进行绘制*/The background can be set using setBackgroundRole() andsetPalette().
Since Qt 4.0, QWidget automatically double-buffers its painting/*自动双缓存*/, so there is no need to write double-buffering code inpaintEvent()to avoid flicker/*避免闪烁*/.
========================================================
2、鼠标事件及相关
void QWidget::mousePressEvent(QMouseEvent * event) [virtual protected]
void QWidget::mouseDoubleClickEvent(QMouseEvent * event) [virtual protected]
void QWidget::mouseReleaseEvent(QMouseEvent * event) [virtual protected]
void QWidget::mouseMoveEvent(QMouseEvent * event) [virtual protected]
void setMouseTracking(bool enable) /* 鼠标跟随 */
Qt::MouseButtons QMouseEvent::buttons() const /*发生事件时,哪些按钮还处于按下状态 */
Returns the button state when the event was generated/*触发*/. The button state is a combination/*组合*/ ofQt::LeftButton,Qt::RightButton,Qt::MidButtonusing theOR operator.
For mouse move events, this is all buttons that are pressed down./*对于移动事件,所有的按钮都是按下状态 */
For mouse press and double click events this includes the button that caused the event.
For mouse release events this excludes the button that caused the event./*对于鼠标释放事件,不包含导致事件发生的按钮 */
Qt::MouseButton QMouseEvent::button() const /*哪个按钮发生了此事件。返回事件的按钮,但是对于鼠标移动事件,总是返回Qt::NoButton */
QPoint QMouseEvent::pos() const
QPoint QMouseEvent::globalPos() const
------------------------------------------------------------------------------------------------------
void Dialog::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton) //鼠标左键按下
qDebug() <<event->pos();
}
void Dialog::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()&Qt::LeftButton) //鼠标左键按下的同时移动鼠标(判断状态)
;
}
void Dialog::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) //鼠标左键释放
;
}
========================================================
3、QPainter
1.画图工具主要有3部分组成:QPainter QPaintDevice QPaintEngine
2.QPainter类提供了画图操作的各种接口;QPaintDevice类提供了画图的空间,由QPainter在上面进行绘制;QPaintEngine提供了画笔QPainter在不同的设备上进行绘制的统一接口。QPainter和QPaintDevice之间使用QPaintEngine进行通讯。
3.QPainter类提供了丰富的操作窗口,可以很方便地绘制各种各样的图形(直线、方形、圆形、文字、图片),与QPainterPath类结合,可以绘制出任意复杂的图形。
4.QPaintDevice类是所有可用QPainter类进行绘制的类的基类,常用的画图容器:QImage、QPixmap、QPicture、QWidget。
------------------------------------------------------------------------------------------------------
5.绘制工具QPainter的典型用法:
★构造一个绘图工具:QPainterpainter(this);
★设置画笔和画刷等:painter.setPen(Qt::blue);
★绘制:painter.drawText(rect(), Qt::AlignCenter, "Qt");
★销毁绘图工具
------------------------------------------------------------------------------------------------------6.常用绘图函数:
voiddrawPath(const QPainterPath & path)
voiddrawPixmap(int x, int y, const QPixmap & pixmap)
========================================================
4、键盘事件
void QWidget::keyPressEvent(QKeyEvent * event) [virtual protected]
void MainWindow::keyPressEvent(QKeyEvent *k)
{
if(k->key() == Qt::Key_A) //判断是否是A键按下
ui->label->setPixmap(QPixmap("://1.png"));
}
========================================================
5、光标样式
QCursor(const QPixmap & pixmap, int hotX = -1, int hotY = -1) /* 构造光标 */
void QGuiApplication::setOverrideCursor(const QCursor & cursor) [static] /* 设置光标 */
QCursor myCursor(QPixmap("://2.png"));
QApplication::setOverrideCursor(myCursor);
========================================================
6、密码回显:
void setEchoMode(EchoMode)
enum QLineEdit::EchoMode —— ... QLineEdit::Password...
========================================================
7、QString QString::trimmed() const
Returns a string that has whitespace removed from the start and the end./*移除字符串前后的whitespace */
Whitespace means any character for which QChar::isSpace() returns true. This includes the ASCII characters'\t', '\n', '\v', '\f', '\r', and' '.
Example:
QString str = " lots\t of\nwhitespace\r\n ";
str = str.trimmed();
// str == "lots\t of\nwhitespace"
========================================================
8、void QDialog::accept() [virtual slot]
Hides the modal dialog and sets the result code to Accepted./*隐藏模式对话框,并且设置代码的执行结果为 Accepted */
...... ......
accept();
..... ......
Widget w;
loginDlg login;
if(login.exec()==QDialog::Accepted)
{
w.show();
return a.exec();
}
========================================================