Qt QWidget 示例 UI moc

el/2024/5/23 1:25:34

新建QWidget testqt 项目 会生成以下文件


xz@xiaqiu:~/study/csdn/day0/testqt$ ls
main.cpp  testqt.pro  testqt.pro.user  widget.cpp  widget.h  widget.ui
xz@xiaqiu:~/study/csdn/day0/testqt$ 

testqt.pro


QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

widget.h


#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp


#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}

main.cpp


#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

widget.ui


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>Widget</class><widget class="QWidget" name="Widget"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>Widget</string></property></widget><resources/><connections/>
</ui>

执行qmake 生成makefile 编译makefile


xz@xiaqiu:~/study/csdn/day0/testqt$ mkdir build
xz@xiaqiu:~/study/csdn/day0/testqt$ cd build/
xz@xiaqiu:~/study/csdn/day0/testqt/build$ qmake ..
xz@xiaqiu:~/study/csdn/day0/testqt/build$ ls
Makefile
xz@xiaqiu:~/study/csdn/day0/testqt/build$ make
/opt/Qt/Qt6/6.2.0/gcc_64/libexec/uic ../widget.ui -o ui_widget.h
g++ -c -pipe -O2 -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../testqt -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtWidgets -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -o main.o ../main.cpp
g++ -c -pipe -O2 -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../testqt -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtWidgets -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -o widget.o ../widget.cpp
g++ -pipe -O2 -std=gnu++1z -Wall -Wextra -dM -E -o moc_predefs.h /opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/features/data/dummy.cpp
/opt/Qt/Qt6/6.2.0/gcc_64/libexec/moc -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB --include /home/xz/study/csdn/day0/testqt/build/moc_predefs.h -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -I/home/xz/study/csdn/day0/testqt -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtWidgets -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I'D:\Soft\Dev\All\Edit_Microsoft_VS\IDE\VC\Tools\MSVC\14.28.29333\ATLMFC\include' -I'D:\Soft\Dev\All\Edit_Microsoft_VS\IDE\VC\Tools\MSVC\14.28.29333\include' -I'C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um' -I'C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt' -I'C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared' -I'C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um' -I'C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt' -I'C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt' ../widget.h -o moc_widget.cpp
g++ -c -pipe -O2 -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../../testqt -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/include -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtWidgets -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtGui -I/opt/Qt/Qt6/6.2.0/gcc_64/include/QtCore -I. -I. -I/opt/Qt/Qt6/6.2.0/gcc_64/mkspecs/linux-g++ -o moc_widget.o moc_widget.cpp
g++ -Wl,-O1 -Wl,-rpath,/opt/Qt/Qt6/6.2.0/gcc_64/lib -o testqt main.o widget.o moc_widget.o   /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Widgets.so /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Gui.so /opt/Qt/Qt6/6.2.0/gcc_64/lib/libQt6Core.so -lpthread -lGL   
xz@xiaqiu:~/study/csdn/day0/testqt/build$ 

生成


xz@xiaqiu:~/study/csdn/day0/testqt/build$ ls
main.o    moc_predefs.h   moc_widget.o  ui_widget.h
Makefile  moc_widget.cpp  testqt        widget.o
xz@xiaqiu:~/study/csdn/day0/testqt/build$ 

ui_widget.h


#ifndef UI_WIDGET_H
#define UI_WIDGET_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication> 
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_Widget
{
public:void setupUi(QWidget *Widget){if (Widget->objectName().isEmpty())Widget->setObjectName(QString::fromUtf8("Widget"));Widget->resize(800, 600);retranslateUi(Widget);QMetaObject::connectSlotsByName(Widget);} // setupUivoid retranslateUi(QWidget *Widget){Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));} // retranslateUi};namespace Ui {class Widget: public Ui_Widget {};
} // namespace UiQT_END_NAMESPACE#endif // UI_WIDGET_H

moc_widget.cpp


#include <memory>
#include "../widget.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'widget.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 68
#error "This file was generated using the moc from 6.2.0. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endifQT_BEGIN_MOC_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
struct qt_meta_stringdata_Widget_t {const uint offsetsAndSize[2];char stringdata0[7];
};
#define QT_MOC_LITERAL(ofs, len) \uint(offsetof(qt_meta_stringdata_Widget_t, stringdata0) + ofs), len 
static const qt_meta_stringdata_Widget_t qt_meta_stringdata_Widget = {{
QT_MOC_LITERAL(0, 6) // "Widget"},"Widget"
};
#undef QT_MOC_LITERALstatic const uint qt_meta_data_Widget[] = {// content:10,       // revision0,       // classname0,    0, // classinfo0,    0, // methods0,    0, // properties0,    0, // enums/sets0,    0, // constructors0,       // flags0,       // signalCount0        // eod
};void Widget::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{(void)_o;(void)_id;(void)_c;(void)_a;
}const QMetaObject Widget::staticMetaObject = { {QMetaObject::SuperData::link<QWidget::staticMetaObject>(),qt_meta_stringdata_Widget.offsetsAndSize,qt_meta_data_Widget,qt_static_metacall,nullptr,
qt_incomplete_metaTypeArray<qt_meta_stringdata_Widget_t
, QtPrivate::TypeAndForceComplete<Widget, std::true_type>>,nullptr
} };const QMetaObject *Widget::metaObject() const
{return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}void *Widget::qt_metacast(const char *_clname)
{if (!_clname) return nullptr;if (!strcmp(_clname, qt_meta_stringdata_Widget.stringdata0))return static_cast<void*>(this);return QWidget::qt_metacast(_clname);
}int Widget::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{_id = QWidget::qt_metacall(_c, _id, _a);return _id;
}
QT_WARNING_POP
QT_END_MOC_NAMESPACE

ui 上添加按钮


xz@xiaqiu:~/study/csdn/day0/testqt$ designer widget.ui 

///图

widget.ui


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>Widget</class><widget class="QWidget" name="Widget"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>Widget</string>/按钮///</property><widget class="QPushButton" name="pushButton"><property name="geometry"><rect><x>160</x><y>200</y><width>141</width><height>101</height></rect></property><property name="text"><string>PushButton</string></property></widget>
/按钮///</widget><resources/><connections/>
</ui>

ui_widget.h

#ifndef UI_WIDGET_H
#define UI_WIDGET_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton> //QPushButton
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_Widget
{
public:QPushButton *pushButton; //QPushButtonvoid setupUi(QWidget *Widget){if (Widget->objectName().isEmpty())Widget->setObjectName(QString::fromUtf8("Widget"));Widget->resize(800, 600);QPushButton///pushButton = new QPushButton(Widget);pushButton->setObjectName(QString::fromUtf8("pushButton"));pushButton->setGeometry(QRect(160, 200, 141, 101));QPushButton///retranslateUi(Widget);QMetaObject::connectSlotsByName(Widget);} // setupUivoid retranslateUi(QWidget *Widget){Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));QPushButton///pushButton->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));QPushButton///} // retranslateUi};namespace Ui {class Widget: public Ui_Widget {};
} // namespace UiQT_END_NAMESPACE#endif // UI_WIDGET_H

添加opencv

testqt.pro添加


linux: LIBS += -L/usr/lib/x86_64-linux-gnu -lopencv_core -lopencv_highgui
INCLUDEPATH += /usr/include/opencv4

main.cpp


#include "widget.h"
#include <QApplication>
#include <opencv2/highgui.hpp>
int main(int argc, char *argv[])
{cv::namedWindow("opencv");QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

http://www.ngui.cc/el/5239661.html

相关文章

手动创建pro

手动创建pro xzxiaqiu:~/study/csdn/day0$ mkdir testqmake xzxiaqiu:~/study/csdn/day0$ cd testqmake/ xzxiaqiu:~/study/csdn/day0/testqmake$ subl testqmake.protestqmake.pro SOURCES main.cpp CONFIG consolemain.cpp #include <iostream> using namespace st…

Qt创建动态库

1.TEMPLATE lib 2.静态库CONFIG staticlib 创建XSocket.cpp XSocket.h testlib.pro SOURCES XSocket.cpp HEADERS XSocket.h TARGET testlib TEMPLATE lib xzxiaqiu:~/study/csdn/day0/testlib$ ls testlib.pro XSocket.cpp XSocket.h xzxiaqiu:~/study/csdn/day0/t…

窗口的状态

窗口的状态-最大化最小化全屏 1.setWindowState(Qt::WindowMaximized); 2.WindowMinimized WindowMaximized WindowNoState WindowFullScreen 3.showMaximized() showMinimized() 4.showNormal() showFullScreen() 定制窗口 1.CustomizeWindowHint 2.setWindowFlags 3.setWind…

字符集字符集

字符集 1.ASCII的七位字符集 128个字符 2.标准ASCII中最高位(b7)用作基偶校验 3.ISO-8859-1扩展ascii 128-255拉丁 ANSI 标准 1.多字节字符集(Multi-ByteChactacter Set) 2.0到127之间的字符&#xff0c;依旧是一个字节代表一个字符 3.2字节来表示一个字符 GB2312 GBK编码 1.…

Qt 字符串查找 字符串截断

Qt 字符串查找 itstr.indexOf("xc") itstr.indexOf("xc",itstr.indexOf("xc")2) itstr.lastIndexOf("xc") itstr.indexOf(QRegExp("[0-9]"));字符串截断 itstr.chop(5); itstr.left(itstr.indexOf("xc")); itstr.…

QPushButton 样式设置 渐变圆角光泽按钮

QPushButton 1.事件设置 2.快捷键设置 3.样式设置 QPushButton事件信号 1.click() 2.click(bool) 是否选中 3.pressed() 4.released() #include <QWidget> #include <QDebug> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACEclass Widget :…

遍历所有QObject的子节点

遍历所有QObject的子节点 1.遍历子节点-获取节点对象的名称 2.QObjectList children() 3.objectName() 4.转换子节点-获取节点对象的类型 5.QMetaObject *metaObject() 6.metaObject()->className(); 7.qobject_cast<QCheckBox*>(cb); ui_widget.h #ifndef UI_WIDGET…

Qt - QRadioButton

QRadioButton 1.类似QCheckBox 2.默认单选 3.布局器 4.QButtonGroup获取选中的Button Widget.cpp #include "widget.h" #include "ui_widget.h" #include <QRadioButton> #include <QVBoxLayout> #include <QButtonGroup> #include <…

Qt事件Event

Qt事件Event 1.事件函数event 2.鼠标事件 3.键盘事件 4.窗口大小变化事件 5.重绘事件 重载event 1.bool event(QEvent *e); 2.返回true 处理下一个事件 3.返回false本事件的下一个处理函数QEvent子类 1.QKeyEvent,QMouseEvent 2.QTouchEvent,QWheelEvent QWidget重载函数 1.…

QPainter融合两张图片渲染

QPainter融合两张图片渲染 QPainter painter(this); out src.copy(); QPainter p(&out); p.setRenderHint(QPainter::Antialiasing); p.drawImage(0,0,img); p.end(); painter.drawImage(0,0,out); painter.end();XPS图片编辑器 1.打开存储图片(菜单) 2.画笔和橡皮 3.画笔…