QT5 Question

closed account (3qX21hU5)
So have been studying QT for a bit and have started to make a few simple programs, but the thing I am suck on is this.

I want to be able to have a window popup (Kind of like a QMessagebox) with a list of information.

For example lets say I wanted to print a list of Celsius to Fahrenhiet conversions from 0-10 like below.

1
2
3
4
5
6
C: 1   F: 1
C: 2   F: 2
C: 3   F: 3
C: 4   F: 4

ect.


The code looking like this (Or something along these lines)

1
2
3
4
5
for (int index = 0; index != 10; ++index)
{
    QString print = QString("C:%1    F:%2").arg(index).arg(convertToF(index));
    cout << print << endl;
}


But anyways my question is how would I go about doing this? What would be the best widget to use? I assume I would need to use QString (And because I love it ;p). So if anyone could point me in the right direction for how to print this out to a separate window then my main window I would appreciate it.
If you just want the information to pop up in a dialog once with no other editing capabilities, I would actually use a QMessageBox.
Here's how I would approach the situation.

1
2
3
4
5
6
7
8
9
10
QString output;
QTextStream stream(output); //text stream is really useful when concatenating long strings
//and especially useful in file input/output. (you have to #include <QTextStream> as well.)
//It's pretty much the Qt version of a stringstream.

for(int index = 0; index != 10; ++index)
    stream << "C:" << index << "    F:" << convertToF(index) << "\n";

//display the message box
QMessageBox::information(this, "Output", output, QMessageBox::Ok);


If you get to a point where you need to output/save a lots of data, it's probably best to make a custom widget with a QTableModel (or something of the sort) as the central widget (which is a lot lengthier than just quickly outputting data with a QMessageBox).
Last edited on
closed account (3qX21hU5)
I'm not sure I get how that works, your streaming from a QString? Would you mind going into a bit more detail of it works?

Would it still keep the correct format (one C: F: per line)? Again just starting with QT so not sure how most of its parts work.
Last edited on
QTextStream simply gives you an interface where you can use << and >> operators on things.

Now that I look over it, that's probably not the most intuitive way to use QTextStream. The following is much better imo.
1
2
3
4
5
6
QTextStream output;

for(int index = 0; index != 10; ++index)
    output << "C: " << index << "    F:" << convertToF(index) << "\n";

QMessageBox::information(this, "Output", output.string(), QMessageBox::Ok);

The first example I gave you is only different in that 'stream' is operating on an existing QString, which I'm just deciding isn't necessary.

If you're familiar at all with the STL's stringstream, it's pretty much used for concatenating strings from various data types, such as ints and other things that an std::string doesn't handle directly. QTextStream does the same thing (and much much more cool things once you dig into it) for QStrings.

So yes, the above will result in the same thing you would get by using cout in a console. The actual string looks like
C: 1    F: 1\nC: 2    F: 2\nC: 3   F: 3\nC:4    F:4\n

However, like the console, QMessageBox handles escape characters like \n, and will display it properly.

Displaying the message box should yield something like

C: 1   F: 1
C: 2   F: 2
C: 3   F: 3
C: 4   F: 4

And give you an "Ok" button to close the QMessageBox and continue execution.

You can check out the docs for QTextStream at http://qt-project.org/doc/qt-5.0/qtcore/qtextstream.html

Also, this page is a really good thing to bookmark when you're working with Qt:
http://qt-project.org/doc/qt-5.0/qtdoc/classes.html

EDIT:
Overall, though, i would be weary of using QT5. They've made some significant changes on some core framework stuff that I just do not like after working with 4.8.4 for ages. The most aggravating to me so far is that it's exceptionally difficult to go about handling native OS events when you need to. That and you can't build libqxt (a super handy extension of Qt) for Qt5 (yet). That, and because Qt5 is so new, there's not many resources or solved problems on the internet for you to reference; it's almost impossible to find good third party references.
Just giving you a heads up in the event that you run into issues with Qt5, it might be easier just to revert to Qt4.8.4, which has all the same functionality, minus C++11 support.
Last edited on
closed account (3qX21hU5)
Thank you so much Thumper it finally clicked.
closed account (3qX21hU5)
Just a quick update on how it went. I tried working with the QTextStream but it wasn't working for me so I eventually settled on something like this

1
2
3
4
5
6
7
8
    QString output;

    for (int index = 0; index != 100; index += 5)
    {
        output += QString("C:%1    F:%2\n").arg(index).arg(convertToF(index));
    }

    chart = QMessageBox::information(0, "Temperature Chart", output, QMessageBox::Ok);
Ah, that works as well!
Glad you got everything the way you want :)
Topic archived. No new replies allowed.