前言:
今天小伙伴们对“qt文本框”都比较珍视,朋友们都想要了解一些“qt文本框”的相关资讯。那么小编在网摘上汇集了一些关于“qt文本框””的相关文章,希望我们能喜欢,看官们一起来学习一下吧!一、文本编辑组件
1、Qt支持三种常用的文本编辑组件
(1)、QLineEdit:单行文本编辑组件
(2)、QTextEdit:多行富文本(有图片、视频等)编辑组件
(3)、QPlainTextEdit:多行普通文本编辑组件
2、Qt中常用文本编辑组件的继承层次图
3、不同文本组件的特性比较
4、Qt中常用文本编辑组件内置功能
1、初探文本编辑组件
#include "MainWindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), lineEdit(this), plainEdit(this), textEdit(this){ resize(600, 420); lineEdit.move(20, 20); lineEdit.resize(560, 100); lineEdit.insert("QLineEdit"); lineEdit.insert("\n"); lineEdit.insert("<img src=\"C:\\Users\\hp\\Desktop\\D.T.png\" />"); plainEdit.move(20, 130); plainEdit.resize(560, 130); plainEdit.insertPlainText("QPlainTextEdit"); plainEdit.insertPlainText("\n"); plainEdit.insertPlainText("<img src=\"C:\\Users\\hp\\Desktop\\D.T.png\" />"); textEdit.move(20, 270); textEdit.resize(560, 130); textEdit.insertPlainText("QTextEdit"); textEdit.insertPlainText("\n"); textEdit.insertHtml("<img src=\"C:\\Users\\hp\\Desktop\\D.T.png\" />");}MainWindow::~MainWindow(){ }
2、NopePad:
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMenuBar>#include <QMenu>#include <QAction>#include <QString>#include <QtGui/QMainWindow>#include <QToolBar>#include <QIcon>#include <QSize>#include <QStatusBar>#include <QLabel>#include <QPlainTextEdit>class MainWindow : public QMainWindow{ Q_OBJECTprivate: QPlainTextEdit mainEdit; QLabel statusLabel; MainWindow(QWidget *parent = 0); MainWindow(const MainWindow& obj); MainWindow* operator = (const MainWindow& obj); bool construct(); bool initMenuBar();//菜单栏 bool initToolBar();//工具栏 bool initStatusBar();//状态栏 bool initinitMainEditor();//编辑窗口 bool initFileMenu(QMenuBar* mb);//文件菜单 bool initEditMenu(QMenuBar* mb);//编辑菜单 bool initFormatMenu(QMenuBar* mb);//格式菜单 bool initViewMenu(QMenuBar* mb);//视图菜单 bool initHelpMenu(QMenuBar* mb);//帮助菜单 bool initFileToolItem(QToolBar* tb); bool makeAction(QAction*& action, QString text, int ket);//菜单项 bool makeAction(QAction*& action, QString tip, QString icon);public: static MainWindow* NewInstance(); ~MainWindow();};#endif // MAINWINDOW_H
#include "MainWindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), statusLabel(this){}bool MainWindow::construct(){ bool ret = true; ret = ret && initMenuBar(); ret = ret && initToolBar(); ret = ret && initStatusBar(); ret = ret && initinitMainEditor(); return ret;}MainWindow* MainWindow::NewInstance(){ MainWindow* ret = new MainWindow(); if((ret==NULL) || (!ret->construct())) { delete ret; ret = NULL; } return ret;}bool MainWindow::initMenuBar()//菜单栏{ bool ret = true; QMenuBar* mb = menuBar();//一定要注意是menuBar(),这是普通成员函数,不是构造函数 ret = ret && initFileMenu(mb);//传一个参数是为了在initFileMenu()函数将menu加入菜单栏 ret = ret && initEditMenu(mb); ret = ret && initFormatMenu(mb); ret = ret && initViewMenu(mb); ret = ret && initHelpMenu(mb); return ret;}bool MainWindow::initToolBar()//工具栏{ bool ret = true; QToolBar* tb = addToolBar("Tool Bar"); tb->setMovable(false); tb->setFloatable(false); tb->setIconSize(QSize(16,16)); ret = ret && initFileToolItem(tb); return ret;}bool MainWindow::initStatusBar()//状态栏{ bool ret = true; QStatusBar* sb = statusBar(); QLabel* label = new QLabel("Made By LGC"); if(label != NULL) { statusLabel.setMinimumWidth(200); statusLabel.setAlignment(Qt::AlignHCenter); statusLabel.setText("Ln:1 Col:1"); label->setMinimumWidth(200); label->setAlignment(Qt::AlignHCenter); sb->addPermanentWidget(new QLabel());//单纯加入分隔符 sb->addPermanentWidget(&statusLabel); sb->addPermanentWidget(label); } else { ret = false; } return ret;}bool MainWindow::initinitMainEditor()//编辑窗口{ bool ret = true; mainEdit.setParent(this); setCentralWidget(&mainEdit); return ret;}/************************************************文件菜单********************************************************/bool MainWindow::initFileMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("File(&F)");//创建文件菜单,(&F)是为了可以Alt+F打开 ret = (menu != NULL); if(ret) { QAction* action = NULL; //New ret = ret && makeAction(action, "New(&N)",Qt::CTRL + Qt::Key_N); if(ret) { menu->addAction(action); } menu->addSeparator(); //Open ret = ret && makeAction(action, "Open(&O)...",Qt::CTRL + Qt::Key_O); if(ret) { menu->addAction(action); } menu->addSeparator(); //Save ret = ret && makeAction(action, "Save(&S)",Qt::CTRL + Qt::Key_S); if(ret) { menu->addAction(action); } menu->addSeparator(); //Save As ret = ret && makeAction(action, "Save As(&A)...",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //print ret = ret && makeAction(action, "Print(&P)...",Qt::CTRL + Qt::Key_P); if(ret) { menu->addAction(action); } menu->addSeparator(); //Exit ret = ret && makeAction(action, "Exit(&X)",0); if(ret) { menu->addAction(action);//将菜单项加入到菜单 } } if(ret) { mb->addMenu(menu);//将菜单加入到菜单栏 } else { delete mb; } return ret;}/************************************************编辑菜单********************************************************/bool MainWindow::initEditMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("Edit(&E)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Undo ret = ret && makeAction(action, "Undo(&U)",Qt::CTRL + Qt::Key_Z); if(ret) { menu->addAction(action); } menu->addSeparator(); //Redo ret = ret && makeAction(action, "Redo(&R)...",Qt::CTRL + Qt::Key_Y); if(ret) { menu->addAction(action); } menu->addSeparator(); //Cut ret = ret && makeAction(action, "Cut(&T)",Qt::CTRL + Qt::Key_X); if(ret) { menu->addAction(action); } menu->addSeparator(); //Copy ret = ret && makeAction(action, "Copy(&C)...",Qt::CTRL + Qt::Key_C); if(ret) { menu->addAction(action); } menu->addSeparator(); //Pase ret = ret && makeAction(action, "Pase(&P)...",Qt::CTRL + Qt::Key_V); if(ret) { menu->addAction(action); } menu->addSeparator(); //Delete ret = ret && makeAction(action, "Delete(&L)",Qt::Key_Delete); if(ret) { menu->addAction(action); } menu->addSeparator(); //Find ret = ret && makeAction(action, "Find(&F)...",Qt::CTRL + Qt::Key_F); if(ret) { menu->addAction(action); } menu->addSeparator(); //Replace ret = ret && makeAction(action, "Replace(&R)...",Qt::CTRL + Qt::Key_H); if(ret) { menu->addAction(action); } menu->addSeparator(); //Goto ret = ret && makeAction(action, "Goto(&G)",Qt::CTRL + Qt::Key_G); if(ret) { menu->addAction(action); } menu->addSeparator(); //Select All ret = ret && makeAction(action, "Select All(&A)",Qt::CTRL + Qt::Key_A); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret;}/************************************************格式菜单********************************************************/bool MainWindow::initFormatMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("Format(&O)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Auto Wrap ret = ret && makeAction(action, "Auto Wrap(&W)",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //Font ret = ret && makeAction(action, "Font(&F)...",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret;}/************************************************视图菜单********************************************************/bool MainWindow::initViewMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("View(&V)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Tool Bar ret = ret && makeAction(action, "Tool Bar(&T)",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //Status Bar ret = ret && makeAction(action, "Status Bar(&S)",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret;}/************************************************帮助菜单********************************************************/bool MainWindow::initHelpMenu(QMenuBar* mb){ bool ret = true; QMenu* menu = new QMenu("Help(&H)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //User Manual ret = ret && makeAction(action, "User Manual",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //About NotePad ret = ret && makeAction(action, "About NotePad...",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret;}bool MainWindow::initFileToolItem(QToolBar* tb){ bool ret = true; QAction* action = NULL; ret = ret && makeAction(action, "New", ":/Res/pic/new.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Open", ":/Res/pic/open.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Save", ":/Res/pic/save.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Save As", ":/Res/pic/saveas.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Print", ":/Res/pic/print.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Undo", ":/Res/pic/undo.png"); if(ret) { tb->addAction(action); } ret = ret && makeAction(action, "Redo", ":/Res/pic/redo.png"); if(ret) { tb->addAction(action); } return ret;}bool MainWindow::makeAction(QAction*& action, QString text, int key)//菜单项{ bool ret = true; action = new QAction(text, NULL); if(action != NULL) { action->setShortcut(QKeySequence(key));//创建快捷键 } else { ret = false; } return ret;}bool MainWindow::makeAction(QAction*& action, QString tip, QString icon){ bool ret = true; action = new QAction("", NULL); if(action != NULL) { action->setToolTip(tip); action->setIcon(QIcon(icon)); } else { ret = false; } return ret;}MainWindow::~MainWindow(){ }
#include <QtGui/QApplication>#include "MainWindow.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow* w = MainWindow::NewInstance(); int ret = -1; if(w != NULL) { w->show(); ret = a.exec(); } delete w; return ret;}二、小结
(1)、Qt支持三种常用的文本编辑组件
(2)、Qt中的文本编辑组件封装了常用的编辑功能
A、QLineEdit:单行文本编辑组件
B、QTextEdit:多行富文本编辑组件
C、QPlainTextEdit:多行普通文本编辑组件
点击领取Qt学习资料+视频教程~「链接」
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #qt文本框