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

来源:互联网 发布:怎么买三毛淘宝小号 编辑:程序博客网 时间:2024/05/21 01:51

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


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

0 0
原创粉丝点击