最近使用QT开发的一些心得,技巧

    QLineEdit没有获得焦点和失去焦点的信号,需要自定义一个继承自QLineEdit的输入框,并重写focusInEvent以及focusOutEvent事件

protected:

    virtual void focusInEvent(QFocusEvent *e);

    virtual void focusOutEvent(QFocusEvent *e);

signals:

    void sig_focusin();

    void sig_focusout();

 

void MyLineEdit::focusInEvent(QFocusEvent *e)

{

    QLineEdit::focusInEvent(e);

    emit sig_focusin();

}

 

void MyLineEdit::focusOutEvent(QFocusEvent *e)

{

    QLineEdit::focusOutEvent(e);

    emit sig_focusout();

}

 

 

    自定义控件中含有一个继承自QLineEdit的输入框,以及QLabel,给QLabel赋值时,需要获取焦点才会刷新显示,在给QLabel赋值的时候,调用update可以解决该问题。

 

    QSqlTableModel连续调用removeColumn或者removeColumns无法达到预期的结果。

 

    在重写QLineEditvirtual void keyPressEvent(QKeyEvent *event);时,不处理的键需要调用QLineEdit::keyPressEvent(event);并且都要执行QWidget::keyPressEvent(event);以免窗口也需要获取该按键信息而无法获取到。

 

    给按钮设置快捷键:btnCancle->setShortcut(Qt::Key_Cancel);

    直接设置快捷键,而不与按钮绑定

QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_F3),this);//快捷键F3绑定删除操作

QObject::connect(shortcut, SIGNAL(activated()), this, SLOT(slt_deleteGoods()));

    需要引用:#include <QShortcut>

 

 

    通过上下键,聚焦窗口中的控件,需要重写virtual void keyPressEvent(QKeyEvent *event);

void GoodsInfo::keyPressEvent(QKeyEvent *event)

{

   switch(event->key())

   {

   case Qt::Key_Up:

       focusNextPrevChild(false);

       break;

   case Qt::Key_Down:

       focusNextPrevChild(true);

       break;

   default:

       QDialog::keyPressEvent(event);//QDialog或者QWidget视具体情况

   }

}

 

    使得一个布局居中的方法,通过在两边addStretch();

QHBoxLayout  mainLayout = new QHBoxLayout(this);

mainLayout->addStretch();

mainLayout->addLayout(layout);//layout是一个需要居中的布局,在本实例中是QVBoxLayout类型

mainLayout->addStretch();

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值