
QTextDocument *document = ui->description->document()
ui->description->setHtml(ui->description->document()->toPlainText())
int number=0
bool found = false
QTextCursor highlightCursor(document)
QTextCharFormat plainFormat(highlightCursor.charFormat())
QTextCharFormat colorFormat = plainFormat
colorFormat.setForeground(Qt::red)
if(ui->description->toPlainText()==""){
QMessageBox::information(this, tr("description first"),
"Sorry, please display the description first!")
}
else{
ui->result->setPlainText("")
QString resultstring="搜索结果:"
QString laststring
/*while循环体是本代码的关键-——开始——*/
while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
if(ui->daxiaocheckBox->isChecked()==true){
highlightCursor = document->find(searchString, highlightCursor,QTextDocument::FindCaseSensitively)
}//这个是实现大小写区分效果的代码,你可能不需要
else
highlightCursor = document->find(searchString, highlightCursor)
if (!highlightCursor.isNull()) {
number++
found = true
highlightCursor.movePosition(QTextCursor::Right,QTextCursor::KeepAnchor,0)
highlightCursor.mergeCharFormat(colorFormat)
laststring=QString::number(highlightCursor.position(),10)
if(ui->onlyTwo->isChecked()==true){
if(number<=1)
resultstring+="n occurrence"+QString::number(number,10)+":—— position:"+QString::number(highlightCursor.position(),10)
}//这个是实现大小写区分效果的代码,你可能不需要
else
resultstring+="n occurrence"+QString::number(number,10)+":—— position:"+QString::number(highlightCursor.position(),10)
}
}
/*关键代码结束*/
if(number>1&&ui->onlyTwo->isChecked()==true)
resultstring+="n occurrence"+QString::number(number,10)+":—— position:"+laststring
ui->result->setPlainText(resultstring)
if (found == false) {
QMessageBox::information(this, tr("Word Not Found"),
"Sorry, the word cannot be found.")
}
else {
QMessageBox::information(this, tr("Word was Found"),
"the word '"+searchString+"' was found for "+QString::number(number,10)+((number>1)? " times":" time"))
}
}
QMenu *pMenu = new QMenu(this)
QAction *pTest1 = new QAction(QIcon("copy.ico"),"复制", this)
QAction *pTest2 = new QAction("删除", this)
QAction *pTest3 = new QAction("标记", this)
QAction *pTest4 = new QAction("取消标记", this)
//把QAction对象添加到菜单上
pMenu->addAction(pTest1)
pMenu->addAction(pTest2)
pMenu->addAction(pTest3)
pMenu->addAction(pTest4)
//添加分隔线
//pMenu->addSeparator()
//设置点击后发送的数据
// pTest1->setData(1)
//连接鼠标右键点击信号
connect(pTest1, &QAction::triggered, this, &sub::ClickCopy)
connect(pTest2, &QAction::triggered, this, &sub::ClickDel)
connect(pTest3, &QAction::triggered, this, &sub::Clicktink)
connect(pTest4, &QAction::triggered, this, &sub::ClickDeltink)
//在鼠标右键点击的地方显示菜单
pMenu->exec(cursor().pos())
//释放内存,若此处不手动释放,则必须等到程序结束时才都能释放
QList list = pMenu->actions()
foreach (QAction* pAction, list)
delete pAction
delete pMenu
有时候我们觉得QMainWindow的标题栏不太适合我们的程序要求,而想设计自己的标题栏。这时在构造QMainWindow的时候我们可以设置标记为Qt::FramelessWindowHint,这时我们将得到一个无标题栏的窗口。但是问题也随之而来:我们无法移动这个窗口(有标题栏的时候我们可以通过点击标题栏实现窗口的拖曳)。这时我们可以通过重载QMainWindow的两个成员函数实现窗口的拖动功能:last 和 pos0分别为MainWindow的成员变量
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
QPoint temp = e->pos()
const int pos_min_x = ui->titleWidget->pos().x()
const int pos_max_x = pos_min_x + ui->titleWidget->width()
const int pos_min_y = ui->titleWidget->pos().y()
const int pos_max_y = pos_min_y + ui->titleWidget->height()
if ( temp.x()>pos_min_x &&temp.x()<pos_max_x &&temp.y()>pos_min_y &&temp.y()<pos_max_y )
{
//只对标题栏范围内的鼠标事件进行处理
QPoint newpos = e->globalPos()
QPoint upleft = pos0 + newpos - last
move(upleft)
}
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
QPoint temp = e->pos()
const int pos_min_x = ui->titleWidget->pos().x()
const int pos_max_x = pos_min_x + ui->titleWidget->width()
const int pos_min_y = ui->titleWidget->pos().y()
const int pos_max_y = pos_min_y + ui->titleWidget->height()
if ( temp.x()>pos_min_x &&temp.x()<pos_max_x &&temp.y()>pos_min_y &&temp.y()<pos_max_y )
{
//只对标题栏范围内的鼠标事件进行处理。
last = e->globalPos()
pos0 = e->globalPos() - e->pos()
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)