1.QDialog 窗口外hide;2.QListWidget item重置问题;3.tableview的lineedit;4.tableview 中心checkbox

本文介绍如何利用QApplication的focusChanged信号解决QDialog置顶后点击窗口外使其隐藏的问题;如何通过重置函数使QListWidget随窗口大小变化而及时更新item的位置;如何调整布局使得QLineEdit随QTableView滚动条移动而更新位置;以及如何在QTableView中完美添加QCheckBox。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.关于QDialog置顶后点击窗口外使dialog hide()的方法

本来点击窗口外,当前dialog会闪烁,提示当前窗口为最顶层,不可切换,但是这里可以灵活的使用

QApplication的focusChanged信号,来触发一些函数,

assiant中的说明如下:

This signal is emitted when the widget that has keyboard focus changed from old to now, i.e.,

because the user pressed the tab-key, clicked into a widget or changed the active window.

Both old and now can be the null-pointer.

The signal is emitted after both widget have been notified about the change through QFocusEvent.

大概意思是:当键盘焦点在两个空间切换是会发送这个信号。因为usr使用tab键切换并激活窗口,有时,old和now都可能为空。

connect(qApp,SIGNAL(focusChanged(QWidget*,QWidget*)),this,SLOT(qApp_focus(QWidget*,QWidget*)));

void Booth_detail::qApp_focus(QWidget *old, QWidget *now)
{
    if(!old || !now)return;
    if(old->objectName() == "self_" && now->objectName() == "_self")
        d->hide();
    //qDebug() << "[" << old << "][" <<now <<"]";
}


采用objectName识别,hide窗口

2.解决 QListWidget随窗口大小变化而及时更新item的位置

        inline void fit(){ list_w->reset();}
        virtual void resizeEvent(QResizeEvent *){
            fit();
        }


    当改变窗口的大小是,会触发fit中的reset函数,来更新
    Reset the internal state of the view.

    Warning: This function will reset open editors, scroll bar positions, selections, etc. Existing     changes     will not be committed. If you would like to save your changes when resetting the      view, you can     reimplement this function, commit your changes, and then call the superclass'         implementation.
    重置 视图内部state
    警告:该函数将会重置 打开编辑器,滚动条的位置,选区的位置,而其他改变将不会生效。
    你可已重写该函数,实现保存state,或更新state
3.解决 QTableView最下方添加QLineEdit,且随窗口大小,滚动条移动,headerview的移动,
    有水平滚动自动上移等等因素,
    更新QLineEdit的位置
        line = new QLineEdit;
        QVBoxLayout *vblayout = new QVBoxLayout;
        vblayout->addWidget(line,0,Qt::AlignBottom);
        vblayout->setSpacing(0);
        vblayout->setContentsMargins(0,0,0,0);
        viewport()->setLayout(vblayout);


    并且学要及时更新,代码如下:
    connect(this->verticalScrollBar(),SIGNAL(valueChanged(int)),this,SLOT(update_layout()));
    void Mutiltabel::update_layout()
    {
        viewport()->layout()->update();
    }


    如此即可成功实现效果。
4.完美解决QTableView中中心添加QCheckBox,并且能够随原来的鼠标样式
   
class ItemDelegate : public QItemDelegate
    {
        Q_OBJECT
    public:
        explicit ItemDelegate(int end, QObject *parent = 0) : QItemDelegate(parent),untill(end){}
        virtual void paint(QPainter * painter,const QStyleOptionViewItem & option, const QModelIndex      & index) const
        {
            int  cc = index.column();
            QItemDelegate::paint(painter,option,index);

            if(cc < untill) return;
            int checked;
            QString secs = index.data( Qt::DisplayRole).toString().trimmed();

            secs.toUpper() == "Y"?checked = 1:checked = 0;
            QStyleOptionButton *checkBoxOption = new QStyleOptionButton();
            checkBoxOption->state |= QStyle::State_Enabled;
            /*根据值Y/N判断是否选中*/
            checked?checkBoxOption->state |= QStyle::State_On:
                    checkBoxOption->state |= QStyle::State_Off;

            /*返回QCheckBox位置*/
            QRect x = option.rect;
            QSize c = QCheckBox().sizeHint();
            x.setX(x.x() + (x.width()  - c.width()) / 2);
            x.setY(x.y() + (x.height() - c.height())/ 2);
            x.setSize(c);

            checkBoxOption->rect = x;

            /*绘制QCheckBox*/
            QApplication::style()->drawControl(QStyle::CE_CheckBox,checkBoxOption,painter);
        }
    private:
        int untill;
    };


    这儿采用的是覆盖的方法,简单粗暴

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值