问题描述
- 我这个有不理解的地方帮我讲讲好吗?
-
#include ""mainwindow.h""#include ""ui_mainwindow.h""#include <QTextFrame>#include <QDebug>#include <QLineEdit>#include <QDialog>#include <QPushButton>#include <QVBoxLayout>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) ui(new Ui::MainWindow){ ui->setupUi(this); QTextDocument *document=ui->textEdit->document(); QTextFrame *rootFrame=document->rootFrame(); QTextFrameFormat format; format.setBackground(Qt::blue); format.setBorderBrush(Qt::red); format.setBorder(3); rootFrame->setFrameFormat(format); QTextFrameFormat frameFormat; frameFormat.setBackground(Qt::lightGray); frameFormat.setMargin(20); frameFormat.setPadding(50); frameFormat.setBorder(6); frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dotted); QTextCursor cursor=ui->textEdit->textCursor(); cursor.insertFrame(frameFormat); QAction *action_textFrame=new QAction(tr(""框架"")this); connect(action_textFrameSIGNAL(triggered())thisSLOT(showTextFrame())); ui->mainToolBar->addAction(action_textFrame); QAction *action_textBlock=new QAction(tr(""文本块"")this); connect(action_textBlockSIGNAL(triggered())thisSLOT(showTextBlock())); ui->mainToolBar->addAction(action_textBlock); QAction *action_font=new QAction(tr(""字体"")this); action_font->setCheckable(true); connect(action_fontSIGNAL(toggled(bool))thisSLOT(setTextFont(bool))); ui->mainToolBar->addAction(action_font); QAction *action_textFind=new QAction(tr(""查找"")this); connect(action_textFindSIGNAL(triggered())thisSLOT(textFind())); ui->mainToolBar->addAction(action_textFind);}void MainWindow::showTextFrame(){ QTextDocument *document=ui->textEdit->document(); QTextFrame *frame=document->rootFrame(); QTextFrame::iterator it; for(it=frame->begin();it.atEnd()==0;++it) { QTextFrame *childFrame=it.currentFrame(); QTextBlock childBlock=it.currentBlock(); if(childFrame) qDebug()<<""frame""; else if(childBlock.isValid()) qDebug()<<""block:""<<childBlock.text(); }}void MainWindow::showTextBlock(){ QTextDocument *document=ui->textEdit->document(); QTextBlock block=document->firstBlock(); qDebug()<<document->blockCount(); for(int i=0;i<document->blockCount();i++) { qDebug() <<tr(""文本块%1文本块首行行号为:%2长度为:%3内容为:"").arg(i).arg(block.firstLineNumber()).arg(block.length()) <<block.text(); block=block.next(); }}void MainWindow::setTextFont(bool checked){ if(checked) { QTextCursor cursor=ui->textEdit->textCursor(); QTextBlockFormat blockFormat; blockFormat.setAlignment(Qt::AlignCenter); cursor.insertBlock(blockFormat); QTextCharFormat charFormat; charFormat.setBackground(Qt::lightGray); charFormat.setForeground(Qt::blue); charFormat.setFont(QFont(tr(""宋体"")12QFont::Boldtrue)); charFormat.setFontUnderline(true); cursor.setCharFormat(charFormat); cursor.insertText(tr(""测试字体"")); }}void MainWindow::textFind(){ QDialog *dlg=new QDialog(this); lineEdit=new QLineEdit(dlg); QPushButton *btn=new QPushButton(dlg); btn->setText(tr(""查找下一个"")); connect(btnSIGNAL(clicked(bool))thisSLOT(findNext())); QVBoxLayout *layout=new QVBoxLayout; layout->addWidget(lineEdit); layout->addWidget(btn); dlg->setLayout(layout); dlg->show();}void MainWindow::findNext(){ QString string=lineEdit->text(); bool isfind=ui->textEdit->find(stringQTextDocument::FindBackward); if(isfind) { qDebug()<<tr(""行号:%1列号:%2"").arg(ui->textEdit->textCursor().blockNumber()). arg(ui->textEdit->textCursor().columnNumber()); }}MainWindow::~MainWindow(){ delete ui;}
解决方案
你不理解的地方,建议标志出来、并说明你的理解与问题。否则,别人给你全部注释代码,基本上是不可能的。
解决方案二:
是应用输出的顺序的问题吗
解决方案三:
看不懂,这到底怎么赚积分啊
解决方案四:
建议先学c++
时间: 2024-09-11 23:58:53