You could use a QTextEdit
and its rich text layout capabilities. You can then generate an html table programmatically:
// https://github.com/KubaO/stackoverflown/tree/master/questions/textedit-columns-37949301
#include <QtWidgets>
template <typename It, typename F>
QString toTable(It begin, It end, int columns, F && format,
const QString & attributes = QString()) {
if (begin == end) return QString();
QString output = QStringLiteral("<table %1>").arg(attributes);
int n = 0;
for (; begin != end; ++begin) {
if (!n) output += "<tr>";
output += "<td>" + format(*begin) + "</td>";
if (++n == columns) {
n = 0;
output += "</tr>";
}
}
output += "</table>";
return output;
}
If you're loading a lot of data into a QTextEdit
, you can create a QTextDocument
in a separate thread, fill it there, and then transfer it to QTextEdit
:
#include <QtConcurrent>
void setHtml(QTextEdit * edit, const QString & html) {
QtConcurrent::run([=]{
// runs in a worker thread
auto doc = new QTextDocument;
doc->setHtml(html);
doc->moveToThread(edit->thread());
QObject src;
src.connect(&src, &QObject::destroyed, qApp, [=]{
// runs in the main thread
doc->setParent(edit);
edit->setDocument(doc);
});
});
}
In a similar manner it's possible to relegate the QTextEdit
's rendering to a worker thread as well, although that's a matter I'd have to address in a separate question. The approach would be akin to the one in this answer.
Let's put it to use:
int main(int argc, char ** argv) {
QApplication app{argc, argv};
double const table[] {
78.0, 78.0, 78.0,
0.0, 0.0, 78.0,
69.0, 56.0, 0.0};
QTextEdit edit;
setHtml(&edit, toTable(std::begin(table), std::end(table), 3,
[](double arg){ return QString::number(arg, 'f', 1); },
"width=\"100%\""));
edit.setReadOnly(true);
edit.show();
return app.exec();
}
QString
, it's about whatever you use to display text: it must support the alignment you desire. Please edit the question to show a self-contained example of how you're doing it - it should be <20 lines long, a singlemain.cpp
. – Kuba hasn't forgotten MonicaQTableWidget
. What are you doing exactly? AQTableWidget
orQTableView
can display perfectly aligned columns! Please tell us exactly what you're trying to do first. Probably the how is completely wrong. – Kuba hasn't forgotten Monica