QT——绘图事件、鼠标事件、QPainter、键盘事件、光标样式、登录框密码回显模式、移除字符串前后的空白、对话框accept()

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();
}

========================================================

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值