QTextEdit
및 리치 텍스트 레이아웃 기능을 사용할 수 있습니다. 당신이 QTextEdit
에 많은 양의 데이터를로드하는 경우
// 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;
}
, 당신은, 별도의 스레드에서 QTextDocument
을 만들 거기 작성하고 QTextEdit
로 전송할 수 있습니다 : 당신은 프로그래밍 방식으로 HTML 테이블을 생성 할 수 있습니다 : 그것뿐만 아니라 작업자 스레드에 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);
});
});
}
, 그것이 문제 비록 내가 별도의 질문을 해결해야 할 것이다. 이 접근법은
the one in this answer과 비슷합니다.
는의가 사용 넣어 보자 :
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();
}
그 스크린 샷에서 무엇입니까? 즉, 텍스트를 어떻게 표시하고 있습니까? 고정 폭 글꼴을 사용하지 않는 이유는 무엇입니까? – peppe
당신은 잘못된 것을 요구하고 있습니다. 그것은'QString'에 관한 것이 아니라 여러분이 텍스트를 표시하기 위해 사용하는 것입니다. 원하는 정렬을 지원해야합니다. 질문을 편집하여 자신이 수행하는 방식에 대한 자체적 인 예를 보여줍니다. 20 행을 넘지 않고 하나의 'main.cpp'만 사용해야합니다. –
@peppe 아, 나는 모노 스페이스 글꼴이 존재한다는 것을 몰랐습니다. QTableWidget에서 모노 스페이스 글꼴을 사용하려면 어떻게해야합니까? – picklechips