QT中main.cpp和mainwindow.cpp

main.cpp有main函数,是入口函数,这个没什么好疑问的。
mainwindow.h mainwindow.cpp是主窗体的头文件和源文件,窗体的一些操作当然要在这里写了.

 

Qt和C++的关系,联系或者区别

  QT是框架,C++是语言。

  1. 框架是为了让语言更加方便地开发

  2. C++最著名的框架有MFC和QT

  3. QT是一个完全的C++程序开发框架,包含类库和跨平台国际化开发工具。QT API和开发工具对所有支持平台都是一致的,从而可以进行独立于平台的程序开发和配置。使得跨平台软件编程直观、简易和方便。 

1) 结果集其实就是查询到的所有记录的集合,在QSqlQuery类中提供了多个函数来操作这个集合,需要注意这个集合中的记录是从0开始编号的

2)  双击mainwindow.ui文件进入设计模式

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的例程,可以将图像转换为灰度图,并降低分辨率。需要在.pro文件添加QT += widgets和QT += gui。 main.cpp ```c++ #include <QApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } ``` mainwindow.h ```c++ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #include <QImage> #include <QSpinBox> #include <QVBoxLayout> #include <QHBoxLayout> #include <QPushButton> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void openImage(); void convertToGray(); void reduceResolution(); private: QLabel *imageLabel; QImage image; QSpinBox *spinBox; }; #endif // MAINWINDOW_H ``` mainwindow.cpp ```c++ #include "mainwindow.h" #include <QFileDialog> #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { imageLabel = new QLabel; imageLabel->setBackgroundRole(QPalette::Base); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setScaledContents(true); QWidget *centralWidget = new QWidget(this); setCentralWidget(centralWidget); QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget); QHBoxLayout *buttonLayout = new QHBoxLayout; QPushButton *openButton = new QPushButton(tr("Open"), this); QPushButton *grayButton = new QPushButton(tr("Convert to Gray"), this); QPushButton *reduceButton = new QPushButton(tr("Reduce Resolution"), this); spinBox = new QSpinBox(this); spinBox->setRange(1, 10); spinBox->setValue(1); buttonLayout->addWidget(openButton); buttonLayout->addWidget(grayButton); buttonLayout->addWidget(reduceButton); buttonLayout->addWidget(spinBox); mainLayout->addWidget(imageLabel); mainLayout->addLayout(buttonLayout); connect(openButton, SIGNAL(clicked()), this, SLOT(openImage())); connect(grayButton, SIGNAL(clicked()), this, SLOT(convertToGray())); connect(reduceButton, SIGNAL(clicked()), this, SLOT(reduceResolution())); } MainWindow::~MainWindow() { } void MainWindow::openImage() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.bmp)")); if (fileName.isEmpty()) return; image.load(fileName); if (image.isNull()) { QMessageBox::information(this, tr("Error"), tr("Cannot load %1.").arg(fileName)); return; } imageLabel->setPixmap(QPixmap::fromImage(image)); } void MainWindow::convertToGray() { if (image.isNull()) return; image = image.convertToFormat(QImage::Format_Grayscale8); imageLabel->setPixmap(QPixmap::fromImage(image)); } void MainWindow::reduceResolution() { if (image.isNull()) return; int factor = spinBox->value(); image = image.scaled(image.width() / factor, image.height() / factor); imageLabel->setPixmap(QPixmap::fromImage(image)); } ``` .pro文件 ```c++ QT += widgets gui TARGET = imageConverter TEMPLATE = app SOURCES += main.cpp \ mainwindow.cpp HEADERS += mainwindow.h ``` 这个例程,使用了Qt的几个基本控件,如QLabel、QSpinBox、QPushButton等,并且利用QImage类进行图像处理。openImage()函数用于打开图片,convertToGray()函数将图片转换为灰度图,reduceResolution()函数将图片降低分辨率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值