《QDebug 2020年10月》

    科技2024-06-29  78

    一、Qt Widgets 问题交流

    1.QDateTime/QDate/QTime在QtConcurrent/std::thread用toString()/currentTime()等接口报错(待解决)

    版本:Qt5.15.1 MSVC 32/64-bit

    代码如下:

    QtConcurrent::run([=]{ QTime time; time.setHMS(1,2,4); qDebug()<<time.toString("hh:mm:sss"); });

    错误有两种情况:

    如果使用 moveToThread 就没出现这种情况,暂时没想到怎么解决。

    在 Qt Bug Tracker 里搜索相关关键字如 gregorian 也有类似情况:https://bugreports.qt.io/browse/QTBUG-85692?jql=text%20~%20%22gregorian%22

    2.QChart 的 Axis setLabelFormat 带中文显示乱码(待解决)

    代码如下:

    QValueAxis *axisX = new QValueAxis; axisX->setLabelFormat(QString::fromLocal8Bit("%d 中文")); chart->setAxisX(axisX, series);

    运行后中文就成了问号:

    根据文档的提示,设置下 locale(默认为 printf 格式化,设置 locale number 后用 QString::asprintf 格式化):

    QChart *m_chart = new QChart(); m_chart->setLocale(QLocale(QLocale::Chinese,QLocale::China)); m_chart->setLocalizeNumbers(true);

    这时候更坑的来了,直接刻度都没有了。

    在 Qt Bug Tracker 找到个类似的情况:https://bugreports.qt.io/browse/QTBUG-81981?jql=text%20~%20%22chart%20locale%22

    但是并没有看到解决方法。 

    在源码中(E:\Qt\qt-everywhere-src-5.15.0\qtcharts\src\charts\axis\chartaxiselement.cpp),可以从 createValueLabels 函数开始看。

    3.QBoxLayout 设置对齐

    可以设置该 LayoutItem 中元素整体的对齐,如果是 AlignCenter ,那么就缩在中间一团了:

    void setAlignment(Qt::Alignment alignment)

    也可以设置包含的 LayoutItem 对齐方式,比如想要 QVBoxLayout 中的某个组件左右居中,就可以设置这个:

    bool setAlignment(QWidget *w, Qt::Alignment alignment) bool setAlignment(QLayout *l, Qt::Alignment alignment)

    此外,如果只是设置对齐方式,如 QVBoxLayout 左对齐或者右对齐,可以设置 direction :

    void setDirection(QBoxLayout::Direction direction)

    如果想让某个  QVBoxLayout 中的所有组件都左右居中,可以参照 setAlignment 的实现自己遍历来设置:

    //源码 bool QLayout::setAlignment(QWidget *w, Qt::Alignment alignment) { int i = 0; QLayoutItem *item = itemAt(i); while (item) { if (item->widget() == w) { item->setAlignment(alignment); invalidate(); return true; } ++i; item = itemAt(i); } return false; } //自己遍历 { for(int i=0;i<ui->verticalLayout->count();i++) ui->verticalLayout->itemAt(i)->setAlignment(Qt::AlignCenter); ui->verticalLayout->invalidate(); }

    二、Qt Quick 问题交流

    1.

    三、其他

    1.

    Processed: 0.010, SQL: 8