跳转到内容
新建笔记

Qt 类概览

类名父类描述代码示例(简单)
QWidget-所有控件的基类,提供基本的容器功能。QWidget *window = new QWidget;
QDialogQWidget模态对话框的基类。QDialog *dialog = new QDialog(window);
QMainWindowQWidget主窗口类,用于创建应用程序的主窗口。QMainWindow *mainWindow = new QMainWindow;
QPushButtonQWidget按钮控件。QPushButton *button = new QPushButton("Click Me", window);
QLabelQWidget显示文本或图片的控件。QLabel *label = new QLabel("Hello, World!", window);
QLineEditQWidget单行文本输入控件。QLineEdit *lineEdit = new QLineEdit(window);
QComboBoxQWidget下拉列表控件。QComboBox *comboBox = new QComboBox(window);
QListWidgetQWidget列表控件,显示一列项目。QListWidget *listWidget = new QListWidget(window);
QTableWidgetQWidget表格控件,显示行列数据。QTableWidget *tableWidget = new QTableWidget(3, 2, window);
QStackedWidgetQWidget堆叠控件,可以切换显示多个子控件。QStackedWidget *stackedWidget = new QStackedWidget(window);
QFileDialogQDialog文件选择对话框。下面提供示例代码
QToolBarQWidget工具栏控件,通常包含一组工具按钮。QToolBar *toolBar = addToolBar("Toolbar");
QStatusBarQWidget状态栏控件,用于显示状态信息。QStatusBar *statusBar = statusBar();
QTreeViewQAbstractItemView树形视图控件,显示层次化的数据。QTreeView *treeView = new QTreeView;
QListViewQAbstractItemView列表视图控件,显示项目列表。QListView *listView = new QListView;
QTextEditQTextDocument多行文本编辑控件,支持富文本格式。QTextEdit *textEdit = new QTextEdit;
QGraphicsViewQWidget图形视图控件,用于显示和交互自定义的图形项。QGraphicsView *graphicsView = new QGraphicsView;
QGraphicsScene-图形场景,用于管理QGraphicsItem对象的集合。QGraphicsScene *scene = new QGraphicsScene;
QPrinter-打印机类,用于发送文档到打印机。QPrinter printer;
QProcessQObject用于创建和管理子进程。QProcess *process = new QProcess;
QThreadQObject用于多线程编程。QThread *thread = new QThread;
QMutexQObject互斥锁,用于线程同步。QMutex mutex;
QSemaphoreQObject信号量,用于控制一定数量的线程访问特定资源。QSemaphore semaphore(1);
QPainter-用于绘制2D图形的类,通常与QWidget或其子类配合使用。QPainter painter(&myWidget);
QPen-定义画笔的属性,如颜色、宽度和线型。QPen pen(Qt::black);
QBrush-定义画刷的属性,用于填充图形。QBrush brush(Qt::blue);
QFont-定义字体的属性。QFont font("Arial", 10);
QColor-表示颜色的类。QColor color(Qt::red);
QImage-表示图像的类,用于处理像素数据。QImage image(100, 100, QImage::Format_RGB32);
QPixmap-表示图像的类,通常用于显示图像。QPixmap pixmap("image.png");
QIcon-表示图标的类,用于显示图标。QIcon icon("icon.png");
QTimerQObject定时器类,用于周期性地触发事件。QTimer *timer = new QTimer(this);
QDateTime-表示日期和时间的类。QDateTime now = QDateTime::currentDateTime();
QDate-表示日期的类。QDate today = QDate::currentDate();
QTime-表示时间的类。QTime nowTime = QTime::currentTime();
QWaitConditionQObject条件变量,用于线程间通信。QWaitCondition condition;
QNetworkAccessManagerQObject管理网络请求的类。QNetworkAccessManager *manager = new QNetworkAccessManager;
QNetworkRequest-表示网络请求的类。QNetworkRequest request(QUrl("http://example.com"));
QNetworkReplyQObject表示网络响应的类。QNetworkReply *reply = manager->get(request);
QSqlDatabase-管理数据库连接的类。QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QSqlQuery-执行SQL语句的类。QSqlQuery query; query.exec("SELECT * FROM table");
QSqlTableModelQAbstractTableModel提供SQL数据库表的模型。QSqlTableModel *model = new QSqlTableModel(this, db);
QSqlError-表示数据库错误。QSqlError error = query.lastError();
QModelIndex-表示模型索引的类。QModelIndex index = model->index(0, 0);
QItemSelectionModel-管理项目选择的模型。QItemSelectionModel *selectionModel = new QItemSelectionModel(model);
QMimeDataQObject封装MIME数据的类,用于拖放操作。QMimeData *mimeData = new QMimeData;
QDragQObject管理拖动操作的类。QDrag *drag = new QDrag(item);
QDropEventQEvent表示拖放释放事件。void MyWidget::dragEnterEvent(QDragEnterEvent *event) { ... }
QClipboardQObject访问系统剪贴板的类。QClipboard *clipboard = QClipboard::clipboard();
QUndoStackQObject管理撤销/重做操作的栈。QUndoStack *undoStack = new QUndoStack(this);
QUndoCommandQObject表示单个撤销/重做命令。class MyCommand : public QUndoCommand { ... };
QGraphicsItemQObject图形项的基类,用于在QGraphicsScene中显示。QGraphicsItem *item = scene->addRect(0, 0, 100, 100);
QGraphicsEllipseItemQGraphicsItem圆形或椭圆形图形项。QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem(0, 0, 100, 50);
QGraphicsPolygonItemQGraphicsItem多边形图形项。QGraphicsPolygonItem *polygon = new QGraphicsPolygonItem;
QGraphicsTextItemQGraphicsItem文本图形项。QGraphicsTextItem *text = new QGraphicsTextItem("Hello, World!", scene);
QGraphicsPixmapItemQGraphicsItem用于显示QPixmap的图形项。QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(&pixmap);
QGraphicsSceneQObject图形场景,包含和管理QGraphicsItem对象。QGraphicsScene *scene = new QGraphicsScene;
QGraphicsViewQWidget图形视图,用于显示QGraphicsScene。QGraphicsView *view = new QGraphicsView(scene);
QGraphicsEffectQObject图形效果,用于给QGraphicsItem添加视觉效果。QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect;
QSettingsQObject用于跨会话存储应用程序设置的类。QSettings settings("MyCompany", "MyApp");
QFontDialogQDialog字体选择对话框。QFontDialog *dialog = new QFontDialog(this);
QColorDialogQDialog颜色选择对话框。QColorDialog *dialog = new QColorDialog(this);
QFileDialogQDialog文件选择对话框。QFileDialog *dialog = new QFileDialog(this);
QPrintDialogQDialog打印对话框,用于配置打印选项。QPrintDialog *dialog = new QPrintDialog(&printer);
QPageSetupDialogQDialog页面设置对话框,用于配置页面布局等。QPageSetupDialog *dialog = new QPageSetupDialog(&printer);
QMessageBoxQDialog消息框,用于显示信息、警告或错误。QMessageBox::information(this, "Title", "Message");
QSystemTrayIconQObject系统托盘图标。QSystemTrayIcon *trayIcon = new QSystemTrayIcon(this);
QMenuQWidget菜单控件,用于创建下拉菜单和上下文菜单。QMenu *menu = new QMenu(this);
QActionQObject菜单项或工具栏按钮。QAction *action = new QAction("Action", this);
QShortcutQObject快捷键,用于绑定键盘快捷方式。QShortcut *shortcut = new QShortcut(QKeySequence("Ctrl+N"), this);
QKeySequence-表示键盘快捷键序列。QKeySequence sequence("Ctrl+S");
QDesktopServicesQObject提供对桌面服务的访问,如打开URL。QDesktopServices::openUrl(QUrl("http://example.com"));
QUrl-表示URL的类。QUrl url("http://example.com");
QNetworkAccessManagerQObject管理网络请求的类。QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkReplyQObject表示网络响应的类。QNetworkReply *reply = manager->get(QNetworkRequest(url));
QNetworkRequest-表示网络请求的类。QNetworkRequest request(QUrl("http://example.com"));
QAuthenticatorQObject用于网络请求的身份验证。QAuthenticator authenticator;
QSslSocketQTcpSocket提供SSL/TLS协议支持的套接字。QSslSocket *sslSocket = new QSslSocket(this);
QSslConfiguration-SSL/TLS配置类。QSslConfiguration config; config.setProtocol(QSsl::TlsV1_2);
QVariant-用于存储不同类型数据的容器。QVariant var = 10;
QVariantMapQMap<QString, QVariant>用于存储键值对的映射,键是字符串,值是QVariant。QVariantMap map; map["key"] = "value";
QJsonDocument-表示JSON文档的类。QJsonDocument doc; doc.setObject(QJsonObject::fromVariantMap(map));
QJsonObjectQVariantMap表示JSON对象的类。QJsonObject obj; obj["name"] = "Qt";
QJsonArray-表示JSON数组的类。QJsonArray arr; arr.append("Qt");
QDataStream-用于高效地序列化和反序列化数据流。QDataStream stream(&data, QIODevice::WriteOnly); stream << value;
QBufferQIODevice用于将数据缓冲区作为输入/输出设备。QBuffer buffer; buffer.open(QIODevice::ReadWrite);
QFileQIODevice表示文件的输入/输出设备。QFile file("example.txt");
QDirQObject表示目录的类,提供目录遍历等功能。QDir dir("path/to/directory");
QDirIteratorQObject用于迭代目录中的文件和目录。QDirIterator iterator(dir); while (iterator.hasNext()) { ... }
QProcessQObject用于创建和管理子进程。QProcess process; process.start("command");
QProcessEnvironment-用于访问和修改系统环境变量。QProcessEnvironment env; env.insert("VAR", "value");
QCoreApplicationQObject应用程序的主控制类,用于非GUI应用程序。int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); ... }
QCommandLineParserQObject解析命令行参数的类。QCommandLineParser parser; parser.addOption(...);
QCommandLineOption-表示单个命令行选项的类。QCommandLineOption option; parser.addOption(option);
QScopedPointer-智能指针,确保对象在其作用域结束时被删除。QScopedPointer<QFile> file(new QFile("example.txt"));
QSharedPointer-共享智能指针,用于在多个对象间共享所有权。QSharedPointer<QFile> file(new QFile("example.txt"));
QWeakPointer-弱智能指针,不增加对象的引用计数。QWeakPointer<QFile> weakFile = file;
QPainterPath-用于定义和操作绘制路径。QPainterPath path; path.moveTo(10, 10); path.lineTo(20, 20);
QTransform-用于2D变换,如旋转、缩放、平移等。QTransform transform; transform.rotate(45);
QPolygonQVector<QPoint>表示多边形顶点的集合。QPolygon polygon; polygon << QPoint(0, 0) << QPoint(10, 0);
QRegion-表示区域,用于复杂的图形裁剪和组合。QRegion region(QRect(0, 0, 100, 100));
QBitmapQPixmap用于1位像素深度的图像,通常用于快速绘制。QBitmap bitmap(100, 100);
QPainter-用于绘制图形界面元素。QPainter painter(&pixmap); painter.drawRect(10, 10, 50, 50);
QMouseEventQEvent表示鼠标事件。void MyWidget::mousePressEvent(QMouseEvent *event) { ... }
QKeyEventQEvent表示键盘事件。void MyWidget::keyPressEvent(QKeyEvent *event) { ... }
QResizeEventQEvent表示窗口或控件大小改变的事件。void MyWidget::resizeEvent(QResizeEvent *event) { ... }
QCloseEventQEvent表示窗口即将关闭的事件。void MyWidget::closeEvent(QCloseEvent *event) { ... }
QGraphicsItemGroupQGraphicsItem表示一组图形项,可以对它们进行批量操作。QGraphicsItemGroup *group = new QGraphicsItemGroup; group->addToGroup(item);
QGraphicsLayoutQObject图形布局,用于自动布局QGraphicsWidget。QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
QGraphicsWidgetQGraphicsItem图形窗口小部件,提供更高级的布局和样式功能。QGraphicsWidget *widget = new QGraphicsWidget;
QGraphicsSceneDragDropEventQEvent表示图形场景中的拖放事件。void MyGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event) { ... }
QGraphicsEllipseItemQGraphicsItem表示一个椭圆或圆形的图形项。QGraphicsEllipseItem *ellipse = scene->addEllipse(0, 0, 100, 50);
QGraphicsPolygonItemQGraphicsItem表示多边形的图形项。QGraphicsPolygonItem *polygonItem = scene->addPolygon(QPolygon(...));
QGraphicsTextItemQGraphicsItem表示文本的图形项。QGraphicsTextItem *textItem = scene->addText("Hello, World!");
QGraphicsViewQWidget视图控件,用于显示QGraphicsScene。QGraphicsView *view = new QGraphicsView(scene);
QHelpEventQEvent表示帮助请求事件。void MyWidget::helpEvent(QHelpEvent *event) { ... }
QPainterPathStroker-用于对路径进行描边操作。QPainterPathStroker stroker; stroker.setWidth(4); QPainterPath strokedPath = stroker.createStroke(path);
QTextDocumentQObject表示文本文档,用于QTextEdit等控件。QTextDocument *document = new QTextDocument(this);
QTextCursorQObject用于文本文档的光标操作。QTextCursor cursor(document); cursor.insertText("Hello, World!");
QTextBlockQObject表示文本文档中的一个块。QTextBlock block = document->firstBlock();
QTextCharFormatQObject表示字符格式。QTextCharFormat format; format.setFontWeight(QFont::Bold);
QTextListQObject表示文本列表。QTextList *list = new QTextList(document);
QSyntaxHighlighterQObject用于文本语法高亮。QSyntaxHighlighter *highlighter = new QSyntaxHighlighter(document);
QTextFrameQObject表示文本框架,可以包含其他文本框架或文本块。QTextFrame *frame = document->rootFrame();
QTextTableQObject表示文本表格。QTextTable *table = cursor.insertTable(5, 2);
QTextDocumentFragment-表示文本文档的一部分,可以插入到文档中。QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml("<html><body><p>Text</p></body></html>"); cursor.insertFragment(fragment);
QAbstractItemModelQObject模型/视图编程中的抽象模型基类。QAbstractItemModel *model = new MyModel(this);
QAbstractTableModelQAbstractItemModel表格模型的抽象基类。QAbstractTableModel *tableModel = new MyTableModel(this);
QAbstractListModelQAbstractItemModel列表模型的抽象基类。QAbstractListModel *listModel = new MyListModel(this);
QStandardItemModelQAbstractItemModel标准的项模型,用于表格和列表。QStandardItemModel *standardModel = new QStandardItemModel(this);
QSortFilterProxyModelQAbstractProxyModel代理模型,用于对模型数据进行排序和过滤。QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
QItemDelegateQObject用于自定义项的绘制和编辑。QItemDelegate *delegate = new MyDelegate(this); view->setItemDelegate(delegate);
QItemEditorCreator-用于创建项编辑器的工厂。QItemEditorCreator<QLineEdit> *creator = QItemEditorFactory::defaultFactoryForType(QVariant::String).value<QItemEditorCreator<QLineEdit>>();
QItemSelectionQList<QItemSelectionRange>表示一系列选中的项目。QItemSelection selection; selection.append(QItemSelectionRange(model->index(0, 0), model->index(0, 2)));
QItemSelectionModelQObject用于管理模型中选中的项目。QItemSelectionModel *selectionModel = new QItemSelectionModel(model, view);
QAbstractItemViewQAbstractScrollArea抽象视图,用于显示模型数据。QAbstractItemView *itemView = new MyItemView(this);
QGraphicsEffectQObject用于给图形项添加视觉效果,如阴影、模糊等。QGraphicsEffect *effect = new QGraphicsDropShadowEffect(scene);
QGraphicsOpacityEffectQGraphicsEffect用于设置图形项的不透明度效果。QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect;
QGraphicsColorizeEffectQGraphicsEffect用于改变图形项的颜色效果。QGraphicsColorizeEffect *colorizeEffect = new QGraphicsColorizeEffect;
QGraphicsBlurEffectQGraphicsEffect用于给图形项添加模糊效果。QGraphicsBlurEffect *blurEffect = new QGraphicsBlurEffect;
QGraphicsAnchor-用于定义图形项之间的锚点,以便于布局。QGraphicsAnchor *anchor = new QGraphicsAnchor(widget1, widget2);
QGraphicsGridLayoutQGraphicsLayout图形布局中的网格布局。QGraphicsGridLayout *layout = new QGraphicsGridLayout;
QGraphicsLinearLayoutQGraphicsLayout图形布局中的线性布局。QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
QGraphicsWidgetQGraphicsObject扩展自QGraphicsObject的图形小部件,支持布局和样式。QGraphicsWidget *widget = new QGraphicsWidget;
QGraphicsObjectQObject图形对象的基类,用于自定义图形项。QGraphicsObject *object = new MyGraphicsObject;
QGraphicsSceneMouseEventQEvent表示图形场景中的鼠标事件。void MyGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { ... }
QGraphicsSceneWheelEventQEvent表示图形场景中的鼠标滚轮事件。void MyGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *event) { ... }
QGraphicsSceneDragDropEventQEvent表示图形场景中的拖放事件。void MyGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event) { ... }
QGraphicsSceneContextMenuEventQEvent表示图形场景中的上下文菜单事件。void MyGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { ... }
QGraphicsSceneHoverEventQEvent表示图形场景中的悬停事件。void MyGraphicsScene::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { ... }
QGraphicsSceneResizeEventQEvent表示图形场景大小改变的事件。void MyGraphicsScene::resizeEvent(QGraphicsSceneResizeEvent *event) { ... }
QGraphicsSceneMoveEventQEvent表示图形场景位置改变的事件。void MyGraphicsScene::moveEvent(QGraphicsSceneMoveEvent *event) { ... }
QGraphicsEllipseItemQGraphicsItem表示一个椭圆或圆形的图形项。QGraphicsEllipseItem *ellipse = scene->addEllipse(QRectF(0, 0, 100, 50));
QGraphicsRectItemQGraphicsItem表示一个矩形的图形项。QGraphicsRectItem *rect = scene->addRect(QRectF(0, 0, 100, 100));
QGraphicsPolygonItemQGraphicsItem表示一个多边形的图形项。QGraphicsPolygonItem *polygon = scene->addPolygon(QPolygonF(...));
QGraphicsPathItemQGraphicsItem表示一个路径的图形项。QGraphicsPathItem *pathItem = scene->addPath(QPainterPath(...));
QGraphicsPixmapItemQGraphicsItem表示一个图像的图形项。QGraphicsPixmapItem *pixmapItem = scene->addPixmap(QPixmap("image.png"));
QGraphicsTextItemQGraphicsItem表示一个文本的图形项。QGraphicsTextItem *textItem = scene->addText("Hello, World!");
QGraphicsSvgItemQGraphicsItem表示一个SVG图像的图形项。QGraphicsSvgItem *svgItem = scene->addSvg("image.svg");