使用QListWidget的Qt命令日志 [英] Qt Command Log using QListWidget
问题描述
我正在尝试在用户界面上构建命令日志。这意味着,当用户单击按钮,选中一个复选框,上传一些图像等时,基本上,每次用户与用户界面进行交互时,该操作都记录在 QListWidget
命令日志中如下所示。基本上,这就是用户运行后ui的外观:
I am trying to build a command log on a user interface. Meaning, when the user click a button, check a box, upload some images etc, basically every time the user interacts with the user interface the action is recorded inside a QListWidget
Command Log shown below. Basically this is how the ui looks as soon as the user run it:
这是我每次用户与ui进行交互时都会尝试达到的目标:
And this is what I am try to achieve everytime the user interacts with the ui:
以下来自构造函数的代码段:
Below snippets of code from the constructor:
mainwindow.h
private:
QListWidget *mNewTextSQLLog;
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mDockWidget_A = new QDockWidget(QLatin1String("Command Log"));
mDockWidget_A->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
mDockWidget_A->setMinimumHeight(30);
// Adding object to the DockWidget
mNewText = new QListWidget;
mNewText->setStyleSheet("background-color: light grey;");
mNewText->setMinimumHeight(50);
mNewText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mDockWidget_A->setWidget(mNewText);
addDockWidget(Qt::BottomDockWidgetArea, mDockWidget_A);
resizeDocks({mDockWidget_A}, {200}, Qt::Horizontal);
}
然后是ui的某些命令,例如,当用户上传时使用 QPushButton
的图像和图像也会显示在 QLabel
上:
And then some command of the ui, for example here is when the user upload images using a QPushButton
and images are also shown on a QLabel
:
void MainWindow::imageOriginlUploadB()
{
dir_Original_B = QFileDialog::getExistingDirectory(this, tr("Choose an image directory to load"),
filesListRight, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if(dir_Original_B.length() > 0){
QImage image;
QDir dirBObj(dir_Original_B);
QStringList filesListRight = dirBObj.entryList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
ui->labelOrigImageB->setPixmap(QPixmap::fromImage(image.scaled(125,125,Qt::KeepAspectRatio,Qt::SmoothTransformation)));
for ( int i = 0 ; i < filesListRight.size() ; i++ )
{
ui->listWidgetOriginalImgB->addItem(filesListRight.at(i));
}
ui->listWidgetOriginalImgB->update();
ui->labelOrigImageB->show();
}
}
void MainWindow::on_originalmgB_clicked()
{
imageOriginlUploadB();
}
或在这里调整 QGraphicsView $ c的大小$ c>使用
QPushButton
:
void MainWindow::on_fitViewBtn_clicked()
{
ui->graphicsViewLX->fitInView(mLeftScene->sceneRect(), Qt::KeepAspectRatio);
ui->graphicsViewRX->fitInView(mRightScene->sceneRect(), Qt::KeepAspectRatio);
}
这是 QCheckBox的激活
:
void MainWindow::on_checkBoxScreen_A_toggled(bool checked)
{
if(ui->checkBoxScreen_A->isEnabled()) {
if(checked)
{
ui->checkBoxScreen_A->setText("Active");
ui->saveToFile_A->setEnabled(true);
ui->saveToFile_A->setStyleSheet("QPushButton{ background-color: green }");
}
else {
ui->checkBoxScreen_A->setText("Inactive");
ui->saveToFile_A->setEnabled(false);
ui->saveToFile_A->setStyleSheet("QPushButton{ background-color: grey }");
}
}
}
如何实现?
非常感谢您指出正确的方向
How to achieve that? Thank you very much for pointing in the right direction
推荐答案
我认为 QListWidget
不太适合用于命令日志-您可能要使用 QPlainTextEdit 或 QTextEdit 代替。 (两者之间的主要差异是QPlainTextEdit已针对显示大量文本进行了优化,但为此付出了代价(不支持QTextEdit提供的一些更高级的文本格式设置功能)
I think QListWidget
isn't quite the right widget to use for a Command Log -- you probably want to use either a QPlainTextEdit or a QTextEdit instead. (The main difference between the two is that QPlainTextEdit is optimized for displaying large amounts of text, at the expense of not supporting some of the fancier text-formatting features provided by QTextEdit)
创建了这两个小部件之一后,将文本添加到日志底部仅仅是只需调用 appendPlainText()(或 append())
Once you've created one of those two widgets, adding text to the bottom of log is just a matter of calling appendPlainText() (or append()) on the widget each time you want to add another line of log-text.
除非您要允许用户编辑命令日志中的文本,否则调用 setReadOnly(true)
在窗口小部件上也是一个好主意。
Unless you want to allow the user to edit the text in the Command Log, calling setReadOnly(true)
on the widget is also a good idea.
(如果您还希望日志视图自动滚动到底部,以便可以看到新添加的文本,也可以在添加文本后调用 myCommandLogWidget-> verticalScrollBar()-> setValue(myCommandLogWidget-> verticalScrollBar()-> maximum());
)
(If you also want the log-view to automatically scroll to the bottom so that the newly-added text will be visible, you can also call myCommandLogWidget->verticalScrollBar()->setValue(myCommandLogWidget->verticalScrollBar()->maximum());
after adding the text)
这篇关于使用QListWidget的Qt命令日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!