QT纯代码文本框

这篇博客主要介绍了如何在QT中通过纯代码方式创建和操作文本框,内容涉及imgprocessor.cpp文件中关于界面图片路径的设置和管理。
摘要由CSDN通过智能技术生成

在这里插入图片描述
在这里插入图片描述

.por

#-------------------------------------------------
#
# Project created by QtCreator 2020-06-03T22:29:01
#
#-------------------------------------------------

QT       += core gui
QT       += printsupport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = 172025230
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
        imgprocessor.cpp \
    aa.cpp

HEADERS  += imgprocessor.h \

```c

aa.h


#ifndef SHOWWIDGET_H
#define SHOWWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QTextEdit>      //文本编辑框
#include <QImage>         //插入图片

class ShowWidget : public QWidget
{
   
    Q_OBJECT
public:
    explicit ShowWidget(QWidget *parent = 0);
    QImage *img;
    QLabel *imageLabel;
    QTextEdit *text;

signals:

public slots:
};

#endif // SHOWWIDGET_H



```c
imgprocessor.h
#ifndef IMGPROCESSOR_H
#define IMGPROCESSOR_H

#include "aa.h"
//#include "finddialog.h"
#include <QMainWindow>
#include <QApplication>
#include <QPushButton>
#include <QRadioButton>
#include <QCheckBox>
#include <QImage>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QSaveFile>
#include <QAction>
#include <QActionGroup>
#include <QComboBox>
#include <QSpinBox>
#include <QToolBar>
#include <QToolButton>
#include <QFontComboBox>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QTextCursor>


class ImgProcessor : public QMainWindow
{
    Q_OBJECT

public:
    ImgProcessor(QWidget *parent = 0);
    ~ImgProcessor();
    void createActions();                      //创建动作
    void createMenus();                        //创建菜单
    void createToolBars();                     //创建工具栏
    void loadFile(QString  filename);          //加载文件
    void mergeFormat(QTextCharFormat);         //合并格式
private:
    //各项菜单栏
    QMenu *fileMenu;                           //文件菜单
    QMenu *zoomMenu;                           //缩放菜单
    QMenu *rotateMenu;                         //旋转
    QMenu *mirrorMenu;                         //镜像
    //放置图片
    QImage img;

    QString fileName;                          //打开文件名
    QString curFileName;                       //保存文件名
    //显示中心部件
    ShowWidget *showWidget;





    //文件菜单项
    QAction *openFileAction;                    //打开文件动作
    QAction *NewFileAction;                     //新建文件
    QAction *SaveAction;                        //保存文件
    QAction *SaveAsAction;                      //另存为文件
    QAction *PrintTextAction;                   //打印文本动作
    QAction *PrintImageAction;                  //打印图片
    QAction *exitAction;                        //退出动作

    //编辑菜单项
    QAction *copyAction;                        //复制动作
    QAction *cutAction;                         //剪贴
    QAction *pasteAction;                       //粘贴

    QAction *zoomInAction;                      //放大
    QAction *zoomOutAction;                     //缩小

    //旋转菜单栏--图像处理
    QAction *rotate90Action;                    //90°
    QAction *rotate180Action;                   //180°
    QAction *rotate270Action;                   //270°

    //镜像菜单栏
    QAction *mirrorVerticalAction;              //垂直镜像
    QAction *mirrorHorizonAction;               //水平镜像
    QAction *undoAction;                        //撤销
    QAction *redoAction;                        //取消撤销

    //工具栏
    QToolBar *fileTool;                         //文件-打开、新建、保存,另存为,打印文本(图像)
    QToolBar *zoomTool;                         //缩放
    QToolBar *rotateTool;                       //旋转
    QToolBar *mirrorTool;                       //镜像
    QToolBar *doToolBar;                        //撤销、回撤--操作

    //文本编辑
    QLabel *fontLabel;                          //字体设置项
    QFontComboBox *fontComboBox;                //字体选框
    QLabel *fontLabel2;                         //大小
    QComboBox *sizeComboBox;                    //大小选框
    QToolButton *boldBtn;                       //加粗按钮
    QToolButton *italicBtn;                     //斜体
    QToolButton *underlineBtn;                  //下划线
    QToolButton *colorBtn;                      //字体颜色

    QToolBar *fontToolBar;                      //字体工具栏

    //排序设置项
    QLabel *listLabel;
    QComboBox *listComboBox;
    QActionGroup *actGrp;
    QAction *leftAction;                        //居左
    QAction *rightAction;                       //局右
    QAction *centerAction;                      //居中
    QAction *justifyAction;                     //两端对齐
    QToolBar *listToolBar;                      //排序工具栏

protected slots:
    void ShowNewFile();                         //新建文件
    void ShowOpenFile();                        //打开文件
    void ShowSaveFile();                        //保存文件
    void ShowSaveAsFile();                      //另存为


    void ShowPrintText();                       //打印文本
    void ShowPrintImage();                      //打印图像
    void ShowZoomIn();                          //放大功能
    void ShowZoomOut();                         //缩小功能
    void ShowRotate90();                        //旋转90°
    void ShowRotate180();
    void ShowRotate270();
    void ShowMirrorVertical();                  //镜像--纵向镜像
    void ShowMirrorHorizontal();                //横向镜像
    void ShowFontComboBox(QString comboStr);    //显示字体框
    void ShowSizeSpinBox(QString spinValue);    //大小自旋盒
    void ShowBoldBtn();                         //加粗功能
    void ShowItalicBtn();                       //显示斜体
    void ShowUnderlineBtn();                    //下划线
    void ShowColorBtn();

    //字符格式化
    void ShowCurrentFormatChanged(const QTextCharFormat &fmt);

    void ShowList(int);                          //排序
    void ShowAlignment(QAction *act);            //对齐方式
    void ShowCursorPositionChanged();            //显示光标位置

};

#endif // IMGPROCESSOR_H







aa.cpp

#include "aa.h"
#include <QHBoxLayout>

ShowWidget::ShowWidget(QWidget *parent) : QWidget(parent)
{
   
    imageLabel = new QLabel;
    //使图片放置合适位置
    imageLabel->setScaledContents (true);

    text = new QTextEdit;

    QHBoxLayout *mainLayout = new QHBoxLayout(this);

    mainLayout->addWidget (imageLabel);
    mainLayout->addWidget (text);
}

imgprocessor.cpp


#include "imgprocessor.h"
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QPrintDialog>
#include <QPrinter>
#include <QPainter>
#include <QColor>
#include <QColorDialog>
#include <QTextList>
#include <QMessageBox>
#include <QDebug>

ImgProcessor::ImgProcessor(QWidget *parent)
    : QMainWindow(parent)
{
   
    setWindowTitle (tr("文本框"));         //设置窗体标题
       //创建放置图像QLabel和文本编辑器QTextEdit的QWidget对象showWidget,并将该QWidget对象设置
       //为中心部件
       resize(800,600);
       showWidget = new ShowWidget(this);
       setCentralWidget (showWidget);


       curFileName = tr("");                     //初始化文件名


    //排序
    listLabel = new QLabel(tr("排序"));
    listComboBox = new QComboBox;
    listComboBox->addItem ("Standard");
    listComboBox->addItem ("QTextListFormat::ListDisc");    //圆盘
    listComboBox->addItem ("QTextListFormat::ListCircle");  //圆
    listComboBox->addItem ("QTextListFormat::ListSquare");  //方形
    listComboBox->addItem ("QTextListFormat::ListDecimal"); //十进制
    listComboBox->addItem ("QTextListformat::ListLowerAlpha"); //小写字母
    listComboBox->addItem ("QTextListFormat::ListUpperAlpha"); //大写字母
    listComboBox->addItem ("QTextListformat::ListLowerRoman"); //小写罗马字符
    listComboBox->addItem ("QTextListFormat::ListUpperRoman"); //大写罗马字符

    //在工具栏上嵌入控件:字体,大小,粗体,斜体,字体颜色
    //字体--这里的字体,字号仅仅是下拉列表框改变
    fontLabel = new QLabel(tr("字体:"));
    fontComboBox = new QFontComboBox;
    //setFontFilter--接口过滤(只在下拉列表框中显示某一类字体,默认情况下为QFontComboBox::AllFonts
    //列出所有字体
    fontComboBox->setFontFilters (QFontComboBox::ScalableFonts);

    //字号
    fontLabel2 = new QLabel(tr("字号:"));
    sizeComboBox = new QComboBox;

    QFontDatabase db;

    //standardSize(): return a list of standard font size(返回可用标准字号的列表).
    foreach (int size, db.standardSizes ())
        sizeComboBox->addItem (QString::number (size)); //将它们插入到字号下拉框中

    //加粗
    boldBtn = new QToolButton;
    boldBtn->setIcon (QIcon("C:/Users/Administrator/Desktop/文本框/172025230/photo/bold.png"));
    //设置成是否是开关(toggle)按钮(true)---即可复选的
    boldBtn->setCheckable (true);

    //斜体
    italicBtn = new QToolButton;
    italicBtn->setIcon (QIcon("C:/Users/Administrator/Desktop/文本框/172025230/photo/italic.png"));
    italicBtn->setCheckable (true);

    //下划线
    underlineBtn = new QToolButton;
    underlineBtn->setIcon (QIcon("C:/Users/Administrator/Desktop/文本框/172025230/photo/underline.png"));
    underlineBtn->setCheckable (true);

    //颜色
    colorBtn = new QToolButton;
    colorBtn->setIcon (QIcon("C:/Users/Administrator/Desktop/文本框/172025230/photo/color.png"));
    colorBtn->setCheckable (true);


    /* 创建动作、菜单、工具栏函数 */
    createActions ();
    createMenus ();
    createToolBars ();
    if (img.load ("C:/Users/Administrator/Desktop/文本框/172025230/photo/image.png"))
    {
   
        //在imageLabel对象放置图像
        showWidget->imageLabel->setPixmap (QPixmap::fromImage (img));
    }

    //连接槽函数
    connect (fontComboBox, SIGNAL(activated(QString)), this, SLOT(ShowFontComboBox(QString)));
    connect (sizeComboBox, SIGNAL(activated(QString)), this, SLOT(ShowSizeSpinBox(QString)));
    connect (boldBtn, SIGNAL(clicked(bool)), this, SLOT(ShowBoldBtn()));
    connect (italicBtn, SIGNAL(clicked(bool)), this, SLOT(ShowItalicBtn()));
    connect (underlineBtn, SIGNAL(clicked(bool)), this, SLOT(ShowUnderlineBtn()));
    connect (colorBtn, SIGNAL(clicked(bool)
### 回答1: Qt是一个跨平台的应用程序开发框架,通过使用Qt,可以方便地进行图形界面的设计和开发。下面是一个使用代码设计登录界面的示例代码(基于Qt C++): ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建主窗口 QWidget *window = new QWidget; window->setWindowTitle("登录界面"); // 创建用户名和密码的文本框 QLineEdit *usernameLineEdit = new QLineEdit; QLineEdit *passwordLineEdit = new QLineEdit; passwordLineEdit->setEchoMode(QLineEdit::Password); // 创建登录按钮 QPushButton *loginButton = new QPushButton("登录"); // 创建布局管理器 QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(new QLabel("用户名:")); layout->addWidget(usernameLineEdit); layout->addWidget(new QLabel("密码:")); layout->addWidget(passwordLineEdit); layout->addWidget(loginButton); window->setLayout(layout); // 连接登录按钮的点击事件 QObject::connect(loginButton, &QPushButton::clicked, [&]() { QString username = usernameLineEdit->text(); QString password = passwordLineEdit->text(); // TODO: 处理登录逻辑 }); // 显示窗口 window->show(); // 运行应用程序 return app.exec(); } ``` 上述示例代码使用Qt的组件类来创建用户界面元素,例如:`QLineEdit`用于创建文本框,`QPushButton`用于创建按钮。使用`QVBoxLayout`布局管理器来自动布局这些界面元素。 在点击登录按钮后,可以通过接收该按钮的点击事件来获取用户名和密码,并执行登录逻辑。 以上是使用代码设计登录界面的一个简单示例,可以根据实际需求进一步添加和修改界面元素。 ### 回答2: Qt代码设计登录界面可以使用QWidget作为登录界面的父窗口,具体实现如下: 1. 创建一个QWidget对象作为登录界面的主窗口,设置窗口大小和标题。 2. 在主窗口中使用QLineEdit控件添加用户名和密码输入框,并设置输入框的样式和大小。 3. 在主窗口中使用QPushButton控件添加登录和退出按钮,并设置按钮的样式和大小。 4. 连接登录按钮的clicked信号与槽函数,用于验证用户名和密码的正确性。 5. 如果用户名和密码正确,弹出登录成功的提示框;否则,弹出登录失败的提示框。 6. 连接退出按钮的clicked信号与槽函数,用于关闭登录界面。 7. 设置主窗口布局管理器,将所有控件添加到布局管理器中进行布局。 8. 设置主窗口的窗口模式为窗口模态,使得登录界面在打开状态下不能操作其他窗口。 9. 调用主窗口的show()函数展示登录界面。 下面是示例代码: ```cpp #include <QApplication> #include <QWidget> #include <QLineEdit> #include <QPushButton> #include <QMessageBox> #include <QVBoxLayout> class LoginWindow : public QWidget { public: LoginWindow(QWidget *parent = nullptr) : QWidget(parent) { // 设置窗口大小和标题 setFixedSize(300, 200); setWindowTitle("登录界面"); // 创建用户名和密码输入框 QLineEdit *usernameEdit = new QLineEdit; QLineEdit *passwordEdit = new QLineEdit; usernameEdit->setPlaceholderText("用户名"); passwordEdit->setPlaceholderText("密码"); usernameEdit->setFixedSize(200, 30); passwordEdit->setFixedSize(200, 30); passwordEdit->setEchoMode(QLineEdit::Password); // 创建登录和退出按钮 QPushButton *loginButton = new QPushButton("登录"); QPushButton *exitButton = new QPushButton("退出"); loginButton->setFixedSize(100, 30); exitButton->setFixedSize(100, 30); // 连接登录按钮的clicked信号与槽函数 connect(loginButton, &QPushButton::clicked, [=]() { QString username = usernameEdit->text(); QString password = passwordEdit->text(); // 验证用户名和密码的正确性 if (username == "admin" && password == "123456") { QMessageBox::information(this, "提示", "登录成功"); } else { QMessageBox::warning(this, "提示", "用户名或密码错误"); } }); // 连接退出按钮的clicked信号与槽函数 connect(exitButton, &QPushButton::clicked, [=]() { close(); }); // 设置布局管理器 QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(usernameEdit); layout->addWidget(passwordEdit); layout->addWidget(loginButton); layout->addWidget(exitButton); setLayout(layout); // 设置窗口模态 setWindowModality(Qt::ApplicationModal); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); LoginWindow loginWindow; loginWindow.show(); return app.exec(); } ``` 以上是使用Qt代码设计登录界面的简单示例,登录界面中包含了用户名和密码的输入框,以及登录和退出按钮。用户在输入正确的用户名和密码后,弹出登录成功的提示框;否则,弹出登录失败的提示框。 ### 回答3: Qt是一个跨平台的C++应用程序开发框架,可以用于构建各种图形用户界面(GUI)应用程序。要使用代码设计Qt登录界面,我们可以按照以下步骤进行: 1. 首先,创建一个Qt应用程序项目并打开Qt Creator。 2. 在Qt Creator中,新建一个QWidget类作为登录界面的窗口。 3. 在窗口的头文件中包含必要的Qt库头文件,并声明所需的成员变量和槽函数。 4. 在窗口的源文件中,实现构造函数和析构函数,并初始化界面和成员变量。 5. 在构造函数中,创建布局管理器(如QVBoxLayout或QGridLayout)并将窗口的中心部分设置为该布局管理器。 6. 在布局管理器中,使用各种Qt控件(如QLabel、QLineEdit和QPushButton)来构建登录界面的各个组件。 7. 使用布局管理器的addLayout()和addWidget()方法将控件添加到布局中。 8. 在槽函数中处理登录按钮的点击事件,验证用户输入的用户名和密码,并根据验证结果执行相应的操作(如弹出提示框或打开主界面)。 9. 最后,将视觉样式(如字体、颜色和大小)应用于控件,并将布局管理器应用于窗口。 10. 编译和运行应用程序,即可看到代码设计的Qt登录界面。 需要注意的是,以上步骤只是大致的设计思路,具体的实现细节还需根据实际需要进行调整。此外,Qt还提供了丰富的控件和功能,可以根据项目需求进行定制和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值