您当前的位置: 首页 > 

龚建波

暂无认证

  • 3浏览

    0关注

    313博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

《QDebug 2021年3月》

龚建波 发布时间:2021-03-09 17:24:44 ,浏览量:3

一、Qt Widgets 问题交流 1.QDateTimeEdit 设置显示的文本或者置为空白

QSpinBox 可以通过 setSpecialValueText 接口设置显示的文本,但是 QDateTime 没有实现该接口。如果是继承 QDateTimeEdit 的话,可以直接操作 lineEdit 成员来设置文本。如果非继承该怎么设置呢?

第一步就是拿到这个 QLineEdit 成员变量指针:

通过 QDateTimeEdit 调用 dumpObjectTree 查看对象数,可以看到有 QLineEdit 子节点,或者查看源码。

ui->dateTimeEdit->dumpObjectTree();

 

void QAbstractSpinBoxPrivate::init()
{
    Q_Q(QAbstractSpinBox);

    q->setLineEdit(new QLineEdit(q));
    edit->setObjectName(QLatin1String("qt_spinbox_lineedit"));
    validator = new QSpinBoxValidator(q, this);
    edit->setValidator(validator);
... ...
}
//protected
void QAbstractSpinBox::setLineEdit(QLineEdit *lineEdit)
{
    Q_D(QAbstractSpinBox);

    if (!lineEdit) {
        Q_ASSERT(lineEdit);
        return;
    }

    if (lineEdit == d->edit)
        return;

    delete d->edit;
    d->edit = lineEdit;
    setProperty("_q_spinbox_lineedit", QVariant::fromValue(d->edit));
    if (!d->edit->validator())
        d->edit->setValidator(d->validator);

    if (d->edit->parent() != this)
        d->edit->setParent(this);

    d->edit->setFrame(!style()->styleHint(QStyle::SH_SpinBox_ButtonsInsideFrame, nullptr, this));
    d->edit->setFocusProxy(this);
    d->edit->setAcceptDrops(false);

    if (d->type != QVariant::Invalid) {
        connect(d->edit, SIGNAL(textChanged(QString)),
                this, SLOT(_q_editorTextChanged(QString)));
        connect(d->edit, SIGNAL(cursorPositionChanged(int,int)),
                this, SLOT(_q_editorCursorPositionChanged(int,int)));
        connect(d->edit, SIGNAL(cursorPositionChanged(int,int)),
                this, SLOT(updateMicroFocus()));
        connect(d->edit->d_func()->control, SIGNAL(updateMicroFocus()),
                this, SLOT(updateMicroFocus()));
    }
    d->updateEditFieldGeometry();
    d->edit->setContextMenuPolicy(Qt::NoContextMenu);
    d->edit->d_func()->control->setAccessibleObject(this);

    if (isVisible())
        d->edit->show();
    if (isVisible())
        d->updateEdit();
}

 所以可以这样获取到 QLineEdit 的指针:

        //QLineEdit *edit=qobject_cast(
        //            ui->dateTimeEdit->property("_q_spinbox_lineedit").value());
        //因为只有一个,所以objectname可以不写
        QLineEdit *edit=ui->dateTimeEdit->findChild("qt_spinbox_lineedit");

拿到指针之后就可以设置文本了。这里面还有个问题,QLineEdit 文本改变后已有的信号会触发相关操作,所以有的版本调用一次 setText 可能无效得调用两次。当然,也可以设置文本前 disconnect QLineEdit 信号槽,设置完了再连上:

        if(edit){
            edit->setText("Test");
            edit->setText("Test");
        }
2.最大化调用 showMinimized() 后,点击任务栏图标没有恢复为最大化状态,呈 Normal 状态显示

参照 QtBug: https://bugreports.qt.io/browse/QTBUG-69711 

Win 下使用 Win32:

ShowWindow((HWND)window->winId(), SW_SHOWMINIMIZED);
二、Qt Quick 问题交流 1.Canvas 绘制圆后 clip 有一圈虚线

解决方法,clip 前 save,clip 后 restore。

三、其他 1.QMessageLogContext 在 release 版本输出文件、函数名、行号等信息

在release模式下,Qt日志的QMessageLogContext默认是不带文件、函数名、行号等信息的,可以在 pro 文件加上:

DEFINES += QT_MESSAGELOGCONTEXT

然后,重新qmake,编译,就可以在release版本中正确输出日志信息。可能需要清理、删除下之前生成的文件才生效。

2.qDebug输出格式化

参照文档:https://doc.qt.io/qt-5/qtglobal.html#qSetMessagePattern

参照博客:http://www.cppblog.com/biao/archive/2013/09/02/202956.html

也可以使用条件 %{if-debug},%{if-info}, %{if-warning},%{if-critical}或%{if-fatal}后跟%{endif},当条件匹配时中间包含的内容才打印。

    QString QT_MESSAGE_PATTERN=
            "[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}]"
            ": %{file}:%{line} - %{message}";
    qSetMessagePattern(QT_MESSAGE_PATTERN);
    qDebug()             
关注
打赏
1655829268
查看更多评论
0.0798s