【问题标题】:How to add a date to QStandardItem in Qt如何在 Qt 中向 QStandardItem 添加日期
【发布时间】:2014-02-06 08:54:28
【问题描述】:

我在 db(timestamp) 中有一个时间字段,例如:2014-12-23 11:55:54 我想在我的 qtgui 中显示这个表。我将表字段存储在向量中。这个“时间”字段的类型是“const char*”。我试过 QString::fromUtf8 和 ascii 和 local8bit。 之后我只在gui中看到了特殊字符。我怎样才能正确地将我的时间字段写入 gui?

顺便说一句,我无法在此处添加代码,因为我们没有互联网连接,我在这里使用移动设备。

【问题讨论】:

  • 你确定向量中的 char* 仍然有效吗? printf/std::cout 显示什么?在 C++ 中,您应该使用 std::string(或 QString)来避免这样的麻烦。
  • 它有效,工作正常。我在不使用 qt 的情况下对其进行了梳理。显示正确的日期。
  • 显示一些代码。你肯定搞砸了。使用基于时间戳创建的QDateTime 添加到QStandardItem 应该是开箱即用的。摆弄const char*QString::fromUtf8 是没有意义的(至少这表明你做错了什么)。
  • 如果它只是纯文本,那么我不明白为什么它不应该工作。请显示一些代码。
  • MyStruct 包含:long long int、short int、char *、short int。我使用 vector 来存储它。当我使用 QStandardItem *temp = new QStandardItem(QString::number(myStruct->at(1).id));工作正常。

标签: c++ qt user-interface char timestamp


【解决方案1】:

首先添加一个变量当前日期和时间

var today = new Date() // contains current date and time

您可以在这里安排宽度高度等设置,也可以描述您的日期格式..

Rectangle {
    width: 200
    height: 200
    Text {
        anchors.centerIn: parent
        text: Qt.formatDateTime(new Date(), "yyMMdd")
    }
}

最后,只需执行您的函数即可获取当前时间和日期。并用 textLabel 显示它们

SystemTime::SystemTime(QWidget *parent)
    : QMainWindow(parent)
{

    //get current date
    QDate date = QDate::currentDate();
    QString dateString = date.toString();
    ui.label->setText("Date: " + dateString);

    //get current time
    QTime time = QTime::currentTime();
    QString timeString = time.toString();
    ui.label_2->setText("Time: " + timeString);

    //get current date and time
    QDateTime dateTime = QDateTime::currentDateTime();
    QString dateTimeString = dateTime.toString();
    ui.label_3->setText("Date and Time: " + dateTimeString);
}

【讨论】:

  • 首先感谢您的长篇回答。但是我已经有了向量中的日期(const char * 格式)我的问题:我无法将它写入我的 gui 中的 tableView。我使用 QStandardItem 来保存其他字段a。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
相关资源
最近更新 更多