Is there any way to get lists of all timezones IST, ET etc.
I have to use them in my application.
The ICU Library is portable and can be used in a Qt application. (It has a C/C++ API.) Among its many other features, is has a TimeZone class that can enumerate the time zones known by the system.
TimeZone Class
It might be overkill if all you need is a simple list, but if you expect to use these time zones and interact with other metadata (locales, etc.), this would be a good solution.
There is a another example using the new QTimeZone class in qt5.2 described here.
They create a custom Widget which lists all known timezones plus their special settings like daylight saving times and such.
The basic code posted there is:
#include <QDebug>
#include <QByteArray>
#include <QDateTime>
#include <QList>
#include <QTimeZone>
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
// Fill in combo box.
QList<QByteArray> ids = QTimeZone::availableTimeZoneIds();
foreach (QByteArray id, ids) {
ui->timeZoneComboBox->addItem(id);
}
// Connect combo box to slot to update fields.
connect(ui->timeZoneComboBox, SIGNAL(currentIndexChanged(int)),
SLOT(UpdateFields()));
// Update fields for initial value.
UpdateFields();
}
void Widget::UpdateFields() {
QByteArray id = ui->timeZoneComboBox->currentText().toLatin1();
QTimeZone zone = QTimeZone(id);
// Fill in fields for current time zone.
if (zone.isValid()) {
ui->descriptionLabel->setText(tr("<b>Description:</b> ") + id);
ui->countryLabel->setText(tr("<b>Country:</b> ") +
QLocale::countryToString(zone.country()));
ui->hasDaylightTimeCheckBox->setChecked(zone.hasDaylightTime());
ui->isDaylightTimeCheckBox->setChecked(
zone.isDaylightTime(QDateTime::currentDateTime()));
ui->hasTransitionsCheckBox->setChecked(zone.hasTransitions());
QDateTime zoneTime = QDateTime(
QDate::currentDate(), QTime::currentTime(), zone).toLocalTime();
ui->dateEdit->setDate(zoneTime.date());
ui->timeEdit->setTime(zoneTime.time());
QTimeZone::OffsetData offset = zone.nextTransition(
QDateTime::currentDateTime());
if (offset.atUtc != QDateTime()) {
ui->nextTransitionLabel->setEnabled(true);
ui->nextTransitionLabel->setText(
tr("<b>Next transition:</b> %1").arg(offset.atUtc.toString()));
} else {
ui->nextTransitionLabel->setEnabled(false);
ui->nextTransitionLabel->setText(
tr("<b>Next transition:</b> none"));
}
}
}
Do you need to somehow find it during runtime, or for your source code? If the second case, you can use this list.
Yes try this example
http://www.developer.nokia.com/Community/Wiki/How_to_get_list_of_Time_Zones_in_Qt_Maemo_application
Related
I've installed VTK 8.2.0 with CMake for use with QT but when trying to run some VTK examples I have some issues shown below:
Note: The colour banding issues in the image is from the gif compression and is not part of the issue
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkCubeSource.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
int main(int, char *[])
{
// Create a cube.
vtkSmartPointer<vtkCubeSource> cubeSource =
vtkSmartPointer<vtkCubeSource>::New();
// Create a mapper and actor.
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(cubeSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actors to the scene
renderer->AddActor(actor);
renderer->SetBackground(.3, .2, .1);
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
https://vtk.org/Wiki/VTK/Examples/Cxx/GeometricObjects/Cube
For all of the examples, it appears that the previous frame is not being cleared and the background is not being shown over it.
I don't appear to get any error messages when compiling or running the program. Might it be a driver issue?
Any help would be much appreciated!
I was trying to use a for each [Modern c++ style] but the code is crashed each time!
It was something like :
for(auto &k:mimeData->formats())
{ ... }
And later out of my surprises I found that the QStringList returned by formats is either invalid or completely separate object though the internal data is ought to be same!
So I tried to figure out in more simple example :
#include <iostream>
#include <string>
#include <list>
#include <QCoreApplication>
#include <QMimeData>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout<<boolalpha;
list<string> ls;
cout<<(ls.begin() != ls.end())<<"\n";
QStringList qsl;
cout<<(qsl.begin() != qsl.end())<<"\n";
QMimeData qme;
cout<<(qme.formats().begin() != qme.formats().end())<<"\n";
cout<<"OMG! is it empty? -> "<<qme.formats().empty()<<"\n";
return a.exec();
}
The output is something like :
false
false
true
OMG! is it empty? -> true
Until or unless I take an rvalue reference I cant decide what is happening internally!
I really need a solution to use it with range based for loops, not Qt's foreach!
P.S. I dont want to copy it to avoid O(n).
Looking at the docs, there's no guarantee QMimeData class keeps QStringList of supported formats (http://doc.qt.io/qt-4.8/qmimedata.html#formats) as a field.
The source code supports that (Qt5.4/Src/qtbase/src/corelib/kernel/qmimedata.cpp:593):
QStringList QMimeData::formats() const
{
Q_D(const QMimeData);
QStringList list;
for (int i=0; i<d->dataList.size(); i++)
list += d->dataList.at(i).format;
return list;
}
Therefore this list is constructed on every call to formats(). Farther calls to it will always yield a separate container.
Since you do need to preserve it to traverse it, I'd recommend keeping a local copy of it. Do note that C++11 allows for it to be moved constructed (or in fact - optimized even better).
My problem is that I have two QTreeWidgets and I would like to do drag and drop from one to an other (and vice-versa). I am able of drag and dropping QTreeWidgetItems, but, when I drop a QTreeWidgetItem that has children, I lose them and only the parent is dropped.
I don't really see how to do it. The only way I have found is reimplementing dropEvent and destroying all teh dropped objects and reconsructing them .. but I don't like that solution because it's slow and the objets are not the same, so it complicates very much the implementation of a Undo/redo feature...
Well, this is what I have:
#include <QApplication>
#include <QDebug>
#include <QObject>
#include <QWidget>
#include <QString>
#include <QTextStream>
#include <QIODevice>
#include <QMainWindow>
#include <QVBoxLayout>
#include "KTreeWidget.h"
#include "KAbstractItem.h"
#include "KItem.h"
#include "KItemGroup.h"
int main(int argc, char* argv[]){
QApplication app(argc, argv);
QMainWindow *window = new QMainWindow();
QWidget* central = new QWidget(window);
QVBoxLayout *layout = new QVBoxLayout();
KTreeWidget* kv = new KTreeWidget(window);
KTreeWidget* trash = new KTreeWidget(window);
layout->addWidget(kv);
layout->addWidget(trash);
central->setLayout(layout);
KItem* kiarr[5];
for (int i = 0 ; i < 5 ; i++){
kiarr[i] = new KItem(kv);
kiarr[i]->setText(0,QString("Item %1").arg(i));
}
KItemGroup* kgarr[5];
for (int i = 0 ; i < 5 ; i++){
kgarr[i] = new KItemGroup(trash);
kgarr[i]->setText(0,QString("Group %1").arg(i));
}
window->setCentralWidget(central);
window->show();
return app.exec();
}
My QTreeWidget:
KtreeWidget.h
#ifndef _KTW_H_
#define _KTW_H_
#include <QTreeWidget>
class KTreeWidget: public QTreeWidget{
Q_OBJECT
private:
QTreeWidgetItem* _header;
public:
KTreeWidget(QWidget* w = NULL);
};
#endif
and KTreeWidget.cc:
#include "KTreeWidget.h"
KTreeWidget::KTreeWidget(QWidget* w):QTreeWidget(w){
setColumnCount(3);
_header = new QTreeWidgetItem(NULL);
_header->setText(0,"Title");
_header->setText(1,"Edit");
_header->setText(2,"Open");
this->setDefaultDropAction(Qt::MoveAction);
setHeaderItem(_header);
setDragEnabled(true);
setAcceptDrops(true);
}
And the items (3 classes, in order to distinguish groups and leafs):
KAbstractItem.h
#ifndef _KABSI_H_
#define _KABSI_H_
#include <QObject>
#include <QTreeWidgetItem>
#include <QTreeWidget>
class KAbstractItem : public QObject, public QTreeWidgetItem{
Q_OBJECT
public:
KAbstractItem(QTreeWidget* p = NULL);
};
#endif
and KAbstractItem.cc
#include "KAbstractItem.h"
KAbstractItem::KAbstractItem(QTreeWidget* p):QTreeWidgetItem(p){}
KItem.h
#ifndef _KI_H_
#define _KI_H_
#include "KAbstractItem.h"
class KItem : public KAbstractItem{
Q_OBJECT
public:
KItem(QTreeWidget* p);
};
#endif
and KItem.cc
#include "KItem.h"
KItem::KItem(QTreeWidget* p):KAbstractItem(p){
setFlags(Qt::ItemIsSelectable
| Qt::ItemIsEditable
| Qt::ItemIsDragEnabled
| Qt::ItemIsUserCheckable
| Qt::ItemIsEnabled);
}
and KItemGroup.h
#ifndef _KIG_H_
#define _KIG_H_
#include "KAbstractItem.h"
class KItemGroup : public KAbstractItem{
Q_OBJECT
public:
KItemGroup(QTreeWidget* p);
};
#endif
and KItemGroup.h
#include "KItemGroup.h"
KItemGroup::KItemGroup(QTreeWidget* p):KAbstractItem(p){
setFlags(Qt::ItemIsSelectable
| Qt::ItemIsEditable
| Qt::ItemIsDragEnabled
| Qt::ItemIsDropEnabled
| Qt::ItemIsUserCheckable
| Qt::ItemIsEnabled);
}
Whenever I do a drop of one of the Items in the first WTreeWifget inside one of the groups of the second one, it works, but it then I move a group to the top QTreeWidget, I lose all the children...
Could you tell me what I am doing wrong?
Thanks in advance!
So, i checked it and it isnt working as required, you are not doing any wrong.
I seems the problem is that QAbstractItemModel (in wich QTreeWidget relies to encode the dragged data internally) mimeData() method is not considering nested items (se that it just encode row + column, but not parent.
It could be even that view is only passing the QModelIndex of the dragged item, to mimeData, thought, it could be better so, cause of not considering parent info...
The only solution i see is that of reimplementing the dropevent. But you dont have to destroy the items.
Use
QTreeWidgetItem * QTreeWidgetItem::takeChild ( int index )
QTreeWidgetItem * QTreeWidget::takeTopLevelItem ( int index )
to take the dragged item
and
void QTreeWidgetItem::insertChild ( int index, QTreeWidgetItem * child )
to drop it
I've checked that this way children are moved right. (See this gist)
This is how I solved it:
When you drag and drop, At creates a new object, differen than yours with the same values for the texts, but it is an other object and yours is simply removed but memory is not freed.
The first thing to do is override removeChild since it is based in indexes and not in addresses so it won't work well with what we are going to do latter. (http://sourceforge.net/p/ckmapper/code/4/tree/trunk/src/KViewer/KAbstractItem.cc#l39)
Then what we need to do is to override the dropEvent of the QTreeWidget. The startegy is the following:
get all the selected items that should be dragged but are not but Qt.
allow Qt to do its dropping.
Find the items on the target that might contain the new objects created by Qt. this can be done with itemAt(event->pos).
Find among the childrne of those items which ones are the dropped ones: this can be done with a dynamic_cast<> because the ones created by Qt are ATreeWidgetItems and ours inherit from this class.
Get its index.
Remove the "parasite" item, and delete it.
insert the selected items at the index from the parasite.
We nedded to override the removeChild because WE are removing children by hand (the selected items) and Qt is removing items (also the selected items) that it should drop latter (but it doesn't). So, if you check the Qt code you will see that it is based in indexes and this is not secure, whereas removing items based on their address is more secure.
I'll post my code latter here, I forgot to commit it ;)
Anyway, Trompa's solution is also valid and seems simpler to me. I just wanted to share my solution too ;)
I have a project "Cybaware_ver1" on QtCreator based on QWidget and everything was working fine. I have a header file with a class containing public and private variables and functions. However, since one day whenever I try to declare a new variable in the header file, even an "int" variable, and run the project, I get
The program has unexpectedly finished.
/home/devdeep/Cybaware_ver1-build-Desktop_Qt_5_0_0_GCC_64bit_SDK-Debug/Cybaware_ver1 exited with code 0
Previously I could add variables in the header file without any problem. Currently, I am able to add new functions, modify UI etc. Just I am unable to add variables to the header file. I have found a temporary solution of declaring these variables as static in the cpp file. However, I would like to find a solution to this.
I have tried re-installing Qt but the problem still persists. I am running it on Ubuntu Linux.
Please let me know if there is a way to fix this. Also, I am not sure what other information I can provide. So please let me know about that.
UPDATE: OK. I used the debugger at it says that the error is a segmentation fault.
It points to the following section of qdebug.h and to the line marked by qt_message_output.
public:
inline QDebug(QIODevice *device) : stream(new Stream(device)) {}
inline QDebug(QString *string) : stream(new Stream(string)) {}
inline QDebug(QtMsgType t) : stream(new Stream(t)) {}
inline QDebug(const QDebug &o):stream(o.stream) { ++stream->ref; }
inline QDebug &operator=(const QDebug &other);
inline ~QDebug() {
if (!--stream->ref) {
if(stream->message_output) {
QT_TRY {
qt_message_output(stream->type,
stream->context,
stream->buffer);
} QT_CATCH(std::bad_alloc&) { /* We're out of memory - give up. */ }
}
delete stream;
}
}
I guess it is a out of memory error. Possible, closing other programs might help?
UPDATE2: Calling "run qmake" did not help. Here is my header file:
#ifndef MAINVIEW_H
#define MAINVIEW_H
#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsLineItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsSimpleTextItem>
#include <list>
#include <QMouseEvent>
#include <QEvent>
#include <QString>
#include <boost/thread.hpp>
#include <qcustomplot.h>
#include <QTimer>
namespace Ui {
class MainView;
}
class MainView : public QWidget
{
Q_OBJECT
public:
int num_missions;
int num_services;
int timer;
int* num_servpermission;
int missionstartx;
//boost::thread pt;
QGraphicsScene mynewscene;
QGraphicsRectItem* myrect;
QGraphicsRectItem** missionline;
QGraphicsLineItem** missionplayline;
QGraphicsRectItem** playrect;
std::list<QGraphicsLineItem**> missionticks;
std::list<QGraphicsEllipseItem**> missionservices;
std::list<QGraphicsSimpleTextItem**> missionservicesno;
std::list<QGraphicsLineItem**>::iterator ti;
std::list<QGraphicsSimpleTextItem**>::iterator ssi;
//std::list<QGraphicsEllipseItem**>::iterator si;
int* missionlength;
int** missionplayxy;
int* missiontickscount;
int* missioniteration;
int missionselected;
int missiontickselected;
int missiontickoffset;
bool isrepeated;
int temp;
QCustomPlot** customPlot;
QTimer dataTimer;
explicit MainView(QWidget *parent = 0);
void playthread();
bool eventFilter(QObject*, QEvent*);
void testfunc();
void clearview();
void test1();
~MainView();
private slots:
void on_startButton_clicked();
void realtimeDataPlot();
void on_pushButton_clicked();
private:
Ui::MainView *ui;
};
#endif // MAINVIEW_H
The program crashes on adding any variable. For example, I added the variable int temp just now and it would crash. Adding anything new makes it crash such as uncommenting the declaration std::list::iterator si;. I take it out and everything works fine.
UPDATE3: My external header file qcustomplot.h is located in the same directory. I am also using boost but that I set up using apt-get. Here is the stack trace:
0 MainView::playthread mainview.cpp 530 0x418fa1
1 boost::_mfi::mf0::operator() mem_fn_template.hpp 49 0x424690
2 boost::_bi::list1 >::operator(), boost::_bi::list0> bind.hpp 253 0x424600
3 boost::_bi::bind_t, boost::_bi::list1 > >::operator() bind_template.hpp 20 0x4245af
4 boost::detail::thread_data, boost::_bi::list1 > > >::run thread.hpp 61 0x42431c
5 thread_proxy /usr/lib/libboost_thread.so.1.46.1 0x7ffff7bcfba9
6 start_thread /lib/x86_64-linux-gnu/libpthread.so.0 0x7ffff6557efc
7 clone /lib/x86_64-linux-gnu/libc.so.6 0x7ffff5af159d
8 ??
The 0th pt in the above trace points to
ui->missionview->invalidateScene()
Here the graphics view gets updated from a function playthread binded to a boost thread. But I am not sure why this would be a problem on adding a variable in the header file as I am not using the variable anywhere in the cpp file.
Go into the project folder and delete the *.pro.user file. Next time you open the project you'll just need to reconfigure it (i.e. choose the appropriate build kit).
I know this is an old thread, but I ran into the same problem myself. Every time I tried to add a variable to my header file it was causing a heap corruption error. I wasn't allocating memory on the heap, or even declaring pointer variables for that matter. It was very frustrating because I didn't know what it was at first. I finally narrowed it down to adding variables to my header file causing the crash. Based on doomguy's experience I knew that it was something persistent in the build folder because he said reinstalling Qt didn't work. I'm glad I read that because I would have been pissed if I reinstalled this behemoth and it still didn't work. Based on prior experience corruption in the .user file can cause issues, and deleting it kinda gives you a clean slate. Moral of the story: If you're getting unusual behavior that seems to point to a problem in the IDE try deleting the .user file before trying more extreme measures.
Not sure if the problem is cause by this, but if the header contains a QObject derived class you should run qmake and after that build your application.
LE: also if this isn't the case, i suggest that you show us a little more code (your code, not Qt code)
Reasonably new to c++, I'm trying to use vectors in my application.
I am using
#include <vector>
in the header file, but when I compile it fails on this line:
std::vector<Shot> shot_list;
Noting the error E2316 'vector' is not a member of 'std'
If I then remove std::, It results in the Undefined symbol 'vector' compiler error message. Really at a loss with this one. Had no issues using
std::list<Shot> shot_list;
prior to using vectors.
Here is a simple example that fails to comile:
//---------------------------------------------------------------------------
#ifndef testclassH
#define testclassH
//---------------------------------------------------------------------------
#include <vector>
class TestClass {
private:
std::vector<int> testVect(1); // removing std:: and adding using namespace std; below the include for the vector it still fails to compile;
};
#endif
To me I don't see any difference between this and This Example
Without clarifying which namespace that the vector is in, you can not use "vector" by itself. (using namespace std;) Maybe you can paste your related code for more spesific help.
Edit:
You can not initialize the vector in the .h. You need to do it in .cpp maybe using the resize() function of the vector. This can be an option for you (using the constructor of the class):
#ifndef testclassH
#define testclassH
//---------------------------------------------------------------------------
#include <vector>
class TestClass {
private:
std::vector<int> testVect;
public:
TestClass()
{
testVect.resize(4);
}
};
#endif
The simple example that you have given compiles if you make the change.