[英]How do I print QStringListModel content?
I need to print the content of a QStringListModel to a printer, in 'void MainWindow::on_pbImprime_clicked()' It's printing any Qstring with no problems, but I don't know how to put the data of QStringListModel to my QString text, anyone have an idea? 我需要将QStringListModel的内容打印到打印机中,在“ void MainWindow :: on_pbImprime_clicked()”中,它可以毫无问题地打印任何Qstring,但是我不知道如何将QStringListModel的数据放入我的QString文本中有一个主意吗?
Here is my code: 这是我的代码:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QStringListModel(this);
ui->lbItens->setModel(model);
ui->lbItens->setEditTriggers(QAbstractItemView::AnyKeyPressed |
QAbstractItemView::DoubleClicked);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pbAdd_clicked()
{
int row = model->rowCount();
model->insertRows(row,1);
QModelIndex index = model->index(row);
ui->lbItens->setCurrentIndex(index);
ui->lbItens->edit(index);
}
void MainWindow::on_pbRemove_clicked()
{
model->removeRows(ui->lbItens->currentIndex().row(),1);
}
void MainWindow::on_pbImprime_clicked()
{
QPrinter printer;
QPainter p(&printer);
int x_pos = 20;
int y_pos = 20;
int row = model->rowCount();
int i;
for(i=0; i<row; i++){
QString text = ;
p.drawText(x_pos, y_pos, text);
y_pos += p.fontMetrics().height();
}
}
Sorry for my bad english and thanks for the help. 对不起,我的英语不好,谢谢你的帮助。
Alternatively, you can try this 或者,您可以尝试此
void MainWindow::on_pbImprime_clicked()
{
QPrinter printer;
...
for(i=0; i<row; i++){
QString text = model->data(model->index(row, 0)).toString();
p.drawText(x_pos, y_pos, text);
y_pos += p.fontMetrics().height();
}
}
As a matter of fact, QVariant QAbstractItemModel::data(const QModelIndex & index, int role = Qt::DisplayRole) const is the prefered way to get data out of model. 实际上,QVariant QAbstractItemModel :: data(const QModelIndex&index,int role = Qt :: DisplayRole)const是从模型中获取数据的首选方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.