有没有人知道如何将时区包含在QDateTime的ISO字符串表示中?
我应该可以使用以下内容:
qDebug() << QDateTime::currentDateTime().toString(Qt::ISODate);
Run Code Online (Sandbox Code Playgroud)
但这总是以UTC格式出现:
2014-02-24T01:29:00Z
Run Code Online (Sandbox Code Playgroud)
目前,我正在解决这个问题的方法是通过显式设置偏移量来强制TimeSpec为Qt :: offsetFromUtc,这是我最初从QDateTime获得的.
QDateTime now = QDateTime::currentDateTime();
int offset = now.offsetFromUtc();
now.setOffsetFromUtc(offset);
qDebug() << now.toString(Qt::ISODate);
Run Code Online (Sandbox Code Playgroud)
这给出了最初的预期:
2014-02-24T01:29:00+02:00
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何以更清洁的方式做到这一点,还是必须将其记录为错误?
编辑:我正在使用Qt5.2.1
更新:
以下小程序显示了我的意思:
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
int main(int argc, int argv){
qDebug() << QDateTime::currentDateTime().toString(Qt::ISODate);
qDebug() << QDateTime::currentDateTime().toTimeSpec(Qt::OffsetFromUTC).toString(Qt::ISODate);
QDateTime now = QDateTime::currentDateTime();
int offset = now.offsetFromUtc();
now.setOffsetFromUtc(offset);
qDebug() << now.toString(Qt::ISODate);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
生成以下输出:
"2014-02-24T10:20:49"
"2014-02-24T08:20:49Z"
"2014-02-24T10:20:49+02:00"
Run Code Online (Sandbox Code Playgroud)
最后一行是预期的那一行.请注意,第二次转换为UTC,这不是想要的.
有没有办法在指定目录(或子文件夹)中的文件触发操作,而不是每次都获取所有修改时间?我问,因为我要检查这个
我正试着试驾这QQmlPropertyMap门课.如果我可以继承它,它似乎可能适用于我想要的东西.这里的文档甚至提供了关于如何对其进行子类化的一些基本指令.所述文档还表明该类派生自QObject.
对于它的价值,我在Qt 5.0.0上使用QtCreator 2.6.1和QtQuick 2.0.
我的main.qml:
import QtQuick 2.0
Rectangle {
width: 360
height: 360
Text {
text: owner.field
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
owner.testFunc();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的main.cpp:
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "TestMap.h"
#include <QQmlContext>
int main(int argc, char *argv[])
{
TestMap* map = new TestMap();
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
QQmlContext* ctxt = viewer.rootContext();
ctxt->setContextProperty("owner", map);
viewer.setMainQmlFile(QStringLiteral("qml/TestMap/main.qml"));
viewer.showExpanded();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
我的TestMap.h
#ifndef TESTMAP_H
#define TESTMAP_H
#include …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已被问过很多次,但我在这里找不到解决方案,也没有在谷歌找到解决方案.
这是我的头文件
#ifndef MAINCONTROLLER_H
#define MAINCONTROLLER_H
#include <QSettings>
#include <QDebug>
#include <QDir>
#include <QObject>
#include "PhTools/PhString.h"
#include "PhStrip/PhStripDoc.h"
class MainController : public QObject
{
Q_OBJECT
public:
explicit MainController(QObject *parent = 0);
void loadSettings();
PhString getLastFile();
void setLastFile(PhString fileName);
bool openDoc(PhString fileName);
signals:
void docChanged();
private:
QSettings * _settings;
PhStripDoc * _doc;
};
#endif // MAINCONTROLLER_H
Run Code Online (Sandbox Code Playgroud)
我的CPP档案:
#include "MainController.h"
MainController::MainController(QObject *parent) :
QObject(parent)
{
_doc = new PhStripDoc();
loadSettings();
}
void MainController::loadSettings()
{
_settings = new QSettings(QDir::homePath() + "/Library/Preferences/com.me.me.plist", QSettings::NativeFormat);
getLastFile(); …Run Code Online (Sandbox Code Playgroud) 对于其他类型,我可以轻松地做类似的事情
mitm.created().toString("yyyy-MM-dd")
Run Code Online (Sandbox Code Playgroud)
是否有类似的功能将qint64转换为QString?你可以在下面找到代码.
fileArray.append("[");
foreach(QFileInfo mitm, mDir.entryInfoList(QDir::Files)){
fileArray.append("{\"filePath\": \"");
fileArray.append(mitm.absoluteFilePath());
fileArray.append("\",");
fileArray.append("\"fileCreated\": \"");
fileArray.append(mitm.created().toString("yyyy-MM-dd"));
fileArray.append("',");
fileArray.append("'fileSize': '");
// fileArray.append(mitm.size());
fileArray.append("\"}");
if(fileCount!=mDir.entryInfoList(QDir::Files).count()-1){ fileArray.append(","); }
fileCount++;
}
fileArray.append("]");
Run Code Online (Sandbox Code Playgroud)
我已经注释掉了破坏代码的行.我有相同的日期问题,但用toString来转换它.我希望qint64会有类似的解决方案.
我最近需要向类添加一个信号,所以我将类更改为继承自QObject并将Q_OBJECT宏添加到类定义中.由于这样做,我在下面的类别行中得到"vtable for CLICommand'的信号未定义引用错误"错误:
// File clicommand.h
#include <QString>
#include <QStringList>
#include <QTcpSocket>
#include "telnetthread.h"
class CLICommand : public QObject
{
Q_OBJECT
public:
CLICommand(TelnetThread *parentTelnetThread);
signals:
void signal_shutdown_request();
private:
TelnetThread *m_parentTelnetThread;
Run Code Online (Sandbox Code Playgroud)
以及第二个错误"在'vtable for CLICommand''的信号未定义的引用错误"在下面的行上(初始化成员变量):
// File clicommand.cpp
#include <QDebug>
#include <QTcpSocket>
#include <QTextStream>
#include "version.h"
#include "clicommand.h"
#include "telnetthread.h"
#include "logger.h"
CLICommand::CLICommand(TelnetThread *parentTelnetThread)
: m_parentTelnetThread(parentTelnetThread)
{
}
Run Code Online (Sandbox Code Playgroud)
就在这里我发出信号的地方.emit行生成对`CLICommand :: signal_shutdown_request()'的错误未定义引用:
// file shutdown_clicommand.cpp
#include <QIODevice>
#include "clicommand.h"
#include "logger.h"
#include "version.h"
void CLICommand::execute_shutdown(const …Run Code Online (Sandbox Code Playgroud) 我使用两个QGLWidgets.一个用于加载纹理,一个用于渲染,但它不起作用.
我在http://blog.qt.digia.com/blog/2011/06/03/threaded-opengl-in-4-8/上使用了以下说明
纹理上传线程上传许多(或大)纹理通常是一项昂贵的操作,因为数据量被推送到GPU.同样,这是可能不必要地阻止主线程的那些操作之一.在4.8中,您可以通过创建一对共享QGLWidgets来解决此问题.其中一个小部件在一个单独的线程中是最新的,但从未在屏幕上显示.主线程通知上传线程要上传哪些图像,上传线程只是在每个图像上调用bindTexture(),然后在每个图像完成时通知主线程,以便可以将其绘制到屏幕上.
使用Qt 4.8和MinGW它工作正常,但现在我使用Qt 5.1和MSVC.当我想让线程中的小部件变为当前时,我收到错误:
无法使QOpenGLContext在另一个线程中保持最新状态
我理解错误,但我该如何解决它.当我没有设置小部件当前我无法加载纹理(在bindTexture()函数冻结).我也想知道,为什么它适用于我的旧QT版本.当出现错误时,我可以按"忽略错误",程序无论如何都会加载纹理.
以下是一些示例代码:
加载纹理:
GLContext::GLContext(QWidget *parent, QGLWidget *myDisplayWidget) :
QGLWidget(parent,myDisplayWidget)
{
}
Run Code Online (Sandbox Code Playgroud)
...
GLContext* myTextureWidget = new GLContext(this,myDisplayWidget);
Run Code Online (Sandbox Code Playgroud)
...
void TextureLoadingThread::run()
{
makeCurrent(); //Here is the bug!
QImage *im = new QImage(filename);
GLuint textid = myTextureWidget->bindTexture(*im, GL_TEXTURE_2D, GL_RGBA);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
当我将myTextureWidget的上下文移动到它运行的线程时,但是当GUI将构建时,我从API获得makeCurrent Error(堆栈跟踪在QT5Widgetsd中的QLineEdit :: setPlaceHolderText函数中说明).当我在主窗口显示几秒后将myTextureWidget移动到线程时,一切正常.但是我怎么知道什么时候qt完成了所有GUI构建的东西?我使用QGLWidget视口将GUI绘制到QGraphicsView.
myTextureWidget->context()->moveToThread(myTextureLoadingThread);
Run Code Online (Sandbox Code Playgroud) 文档只是说明了它,但没有解释原因:
Qt在进入foreach循环时自动获取容器的副本.如果在迭代时修改容器,则不会影响循环.(如果你不修改容器,副本仍然会发生,但由于隐式共享复制容器非常快.)因为foreach创建容器的副本,使用非const引用变量不允许你修改原始容器.它只影响副本,这可能不是你想要的.
对我而言,它看起来像是一种自我强加的障碍,使得Qt foreach没有它本来可行的那么有用 - 现在你不能用它来修改元素.
我听说过boost foreach和新的C++ 11 for (auto iter : array)没有执行副本(虽然我不熟悉其中任何一个).
那么复制背后的理由是什么?
我想知道,如何在QCommandLineParser中使用多个或子参数?例如:
/home/my_app --my_option_with_two_params first_param second_param --my-option-with-one-param param?
Run Code Online (Sandbox Code Playgroud)