Qt_Class

Common 

Words 


>MOC meta object compiler


Undertsanding

>qtmain Library

qtmain is a helper library that enables the developer to write a cross-platform main() function on Windows and on the Symbian platform.

If you do not use qmake or other build tools such as CMake, then you need to link against the qtmain library.

---Common End---


UI 

QDialog 

>adjustSize() adjust the size to its context if the context has set the size

>QCursor::pos() the mouse position

>QApplication::desktop() / QDesktopWidget::screenGeometry() / QDesktopWidget::availableGeometry()

>setAttribute(Qt::WA_DeleteOnClose, true) Dialog will be destroyed after close(), but NOT Null

>setWindowFlags((windowFlags() &~ Qt::WindowMinimizeButtonHint) | Qt::WindowContextHelpButtonHint | Qt::MSWindowsFixedSizeDialogHint);

&~ remove the flag, | add the flag, this is no Min button but with Help Button and size is fixed.

>raise() & activateWindow()

Raises this widget to the top of the parent widget's stack. 

Sets the top-level widget containing this widget to be the active window.

>showNormal() Restores the widget after it has been maximized or minimized

>setWindowFlags(Qt::Popup);

enum Qt::WindowType or flags Qt::WindowFlags

>setGemmetry() & move()

if visible, setGemmetry() will set the positon and size, receive the moveEvent() and/or resizeEvent()


TaskBar icon 

setParent of the main window, the icon will be the parent's, won't create new one.


QMenu 

>SubMenu

    QMenu mSystemTrayMenu;

    QMap<int, QAction*> mActionMap;

    QMenu* pDesktopSubMenu = mSystemTrayMenu.addMenu(tr("&Desktop"));

    mActionMap[1] = pDesktopSubMenu->menuAction();

    mActionMap[2] = CreateAction(tr("&BalloonA"), false, 2);

    pDesktopSubMenu->addAction(mActionMap.value(2));


QSystemTrayIcon 

>isSystemTrayAvailable()

---UI end---


Meta 

Q_INVOKABLE 

QMetaObject::invokeMethod().

 ---Meta End---


APP 

QApplication 

>setQuitOnLastWindowClosed()

default is true, App quits when the last visible primary window with the Qt::WA_QuitOnClose attribute set is closed.

QProcess 

>RunCommand

QString cmd = ("explorer.exe " + nativeFullDirPath);

QProcess::startDetached(cmd);

>Imp

QString exeFileWithQuotes = QString("\"%1\"").arg(exeFile);

QStringList args;

args.append("/restarting");

QProcess adSyncProcess;

adSyncProcess.startDetached(exeFileWithQuotes, args);

 

QScopedPointer
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon destruction.

 QScopedPointer<QApplication> app(createApplication(argc, argv));

 return app->exec();

 ---APP End---


Network 

QNetworkAccessManager 

replace the QHttp

>delete

Note: After the request has finished, it is the responsibility of the user to delete the QNetworkReply object at an appropriate time. Do not directly delete it inside the slot connected to finished(). You can use the deleteLater() function.

Note: Do not delete the reply object in the slot connected to this signal. Use deleteLater().

Destroys the QNetworkAccessManager object and frees up any resources. Note that QNetworkReply objects that are returned from this class have this object set as their parents, which means that they will be deleted along with it if you don't call QObject::setParent() on them.

>deleteLater for reply

Note: It is safe to call this function more than once; when the first deferred deletion event is delivered, any pending events for the object are removed from the event queue.

http://qt-project.org/forums/viewthread/14257 

--- Network End--- 


Other 

EventFilter 

QObject::installEventFilter

Example: monitoredObj->installEventFilter(filterObj);


QtScript 

    QFile qFile(":ToolTipStyle.qs");

    if (qFile.open(QIODevice::ReadOnly | QIODevice::Text))

    {

        QTextStream stream(&qFile);

        QString s = stream.readAll();

        qApp->setStyleSheet(s);

    }

    qFile.close();


QString 

>QString to const char *

toAscii()  fromAscii()

8-bit representation of the string as a QByteArray

toUtf8() fromUtf8()

UTF-8 is a Unicode codec and can represent all characters in a Unicode string like QString


QDataStream

>Output to file

QFile file1("F://file1.txt");

file1.open(QIODevice::WriteOnly);

QDataStream out1(&file1);   // we will serialize the data into the file

out1 << QString("TestApp_ShowMaterialEditor In ").toUtf8();  

---Other End---

`QT_FORWARD_DECLARE_CLASS` 是 Qt 中的前置声明类的宏。 ### 作用 - **节省编译时间**:`#include` 会使编译器展开对应头文件的所有内容进行编译,而前置声明只需要编译用到的部分(一般是一个指针,指向所指的内容部分)即可,从而节省编译时间[^2]。 - **减少依赖**:使用前置声明可以减少头文件之间的依赖,避免不必要的重新编译。当被前置声明的类的定义发生变化时,只要接口不变,包含前置声明的文件不需要重新编译。 ### 使用方法 使用 `QT_FORWARD_DECLARE_CLASS` 宏非常简单,只需要在需要前置声明类的头文件中使用该宏,并将类名作为参数传入即可。例如,要前置声明 `QTableView` 类,可以这样写: ```cpp QT_FORWARD_DECLARE_CLASS(QTableView) ``` 以下是一个更完整的示例,展示了如何在代码中使用 `QT_FORWARD_DECLARE_CLASS`: ```cpp // A.h #pragma once #include <list> #include <vector> #include <map> #include <utility> // 前置声明类 C QT_FORWARD_DECLARE_CLASS(C) namespace test2 { class A { public: C useC(C); C& doToC(C&); C& doToC2(C& c) { return doToC(c); } private: std::list<C> _list; std::vector<C> _vector; std::map<C, C> _map; C* _pc; C& _rc; }; } ``` 在这个示例中,使用 `QT_FORWARD_DECLARE_CLASS(C)` 前置声明了类 `C`,避免了直接包含 `C.h` 头文件,从而减少了编译时间和依赖。 ### 缺点 当修改了源码时,可能不重新编译所修改的内容。所以在写一些库,或者一些大型一点的工程时,基本不建议使用前置声明。对于不是很熟悉的程序员,尽可能地避免使用前置声明,使用 `#include` 包含需要的头文件即可[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值