Qt 编程继续为大家讲解,还是接着文章 Qt 编程点滴 初学者必看 (9),继续介绍,说编程那些细节。由于本话题是一节一节为大家介绍的,所以更多内容请看末尾编辑推荐。
qss 收集:Setting QObject properties
- From 4.3 and above, any designable Q_PROPERTY can be set using the qproperty- syntax.
For example,
- MyLabel { qproperty-pixmap: url(pixmap.png); }
- MyGroupBox { qproperty-titleColor: rgb(100, 200, 100); }
- QPushButton { qproperty-iconSize: 20px 20px; }
- If the property references an enum declared with Q_ENUMS, you should reference its constants by name, i.e., not their numeric value.
类成员函数里定义的临时指针不能做为参数传递;函数里取得的指针无法返回到外面;
- groupmanage.h:
- class GroupManage
- {
- private:
- /// 组在线数据
- /** . */
- class OnLineData
- {
- public:
- IGPSNav* iGpsNav; /**< GPS聊天接口 */
- int groupId; /**< 组ID */
- };
- QList onLineList; /**< 组在线列表 */
- private:
- ///查找GPS聊天接口
- /**
- @param groupId 组id
- @param gpsNav(返回值) 只有在查到时才返回指针
- @return true:查找到 false: 未查找到
- */
- bool findGpsNav(int groupId,unsigned int & gpsNav); //正确的写法:返回对象指针地址
- bool findGpsNav(int groupId,IGPSNav* iGpsNav);//如果是这样写法请注意调用方法
- void test();
- };
- groupmanage.cpp:
- //查找GPS聊天接口
- bool GroupManage::findGpsNav(int groupId,unsigned int & gpsNav)
- {
- bool ok = false;
- for (int i=0; i < onLineList.count() ; i++)
- {
- OnLineData* onLineData = onLineList.at(i);
- if ( onLineData->groupId == groupId )
- {
- ok = true;
- gpsNav = (unsigned int)(onLineData->iGpsNav);
- break;
- }
- }
- _EDEBUG("findGpsNav gpsNav="< return ok;
- }
- bool GroupManage::findGpsNav(int groupId,IGPSNav* iGpsNav)
- {
- bool ok = false;
- for (int i=0; i < onLineList.count() ; i++)
- {
- OnLineData* onLineData = onLineList.at(i);
- if ( onLineData->groupId == groupId )
- {
- ok = true;
- iGpsNav = onLineData->iGpsNav;
- break;
- }
- }
- _EDEBUG("findGpsNav gpsNav="< return ok;
- }
- void GroupManage::test()
- {
- IGPSNav* iGpsNav;
- unsigned int iGpsNavInt;
- bool ok;
- ok = findGpsNav(groupId,iGpsNavInt);
- iGpsNav = (IGPSNav*)iGpsNavInt;
- _EDEBUG("iGpsNav:"<
- /*--下面的写法指针返回不了
- IGPSNav* iGpsNav; //把此句申明放在类的private:下就可以解决问题
- bool ok;
- ok = findGpsNav(groupId,iGpsNav);
- _EDEBUG("iGpsNav:"< */
- }
- QPushButton{
- background-color: rgba( 255, 255, 255, 50% );
- }
透明度50%
1.问题1: connect函数不存在,对象没有从QObject继承.
2.问題2
采用Event方法连接,在对象方法里启动定时器,在运行时发生:
- QT timers cannot be started from another thread
解决:采用Signal/Slot并且连接方式采用 Qt::QueuedConnection
3.利用Signal/Slot删除对象本身:
解决:采用Signal/Slot并且连接方式采用 Qt::QueuedConnection,这时在另一个对象中就可以删除对象本身了。用qss增加每个菜单项的高度,在用StyleSheet美化QMenu时,如何指定菜单项与快捷键的间距?
- QMenu::item {
- border: 1px solid transparent;
- margin: 0px 2px 0px 2px;
- padding: 2px 9px 2px 30px; // Top Right Bottom Left(用这种方式能增加menu中每一行的高度,这就使得在小的触摸屏上进行菜单项的选择的时候,不会那么困难了)
- }
- QGraphicsView无法接收到一些widget传过来的消息的处理方法:
- bool event ( QEvent * event )
- {
- if(event->type()==QEvent::MouseButtonPress)
- {
- QMouseEvent *ke = static_cast(event);
- qDebug()<<"Base Mouse Pressed...." QWidget::event(event); //关键语句
- return true;
- }
- return QGraphicsView::event(event);
- };
基于Signal/Slot机制的接口写法:
- MyInterface: public QObject{ //QObject子类都可以
- Q_OBJECT
- public:
- explicit MyInterface(QObject* parent = 0); //防止出现异常 显式
- virtual ~MyInterface(){}
- virtual void myFunc(int i) = 0; //纯虚函数
- //....
- signals:
- void mySignal(int i);
- //....
- public slots:
- virtual void mySlot(){//do nothing};
- private:
- //...
- }
- MyImpl:public MyInterface{
- Q_OBJECT
- public:
- MyImpl(parent = 0);
- virtual MyImpl(){};
- virtual void myFunc(int i){emit mySignal();};
- public slots:
- void mySlot(){//my code};
- }
- MyImpl2:public MyInterface{
- Q_OBJECT
- public:
- MyImpl(parent = 0);
- virtual MyImpl(){};
- virtual void myFunc(int i){emit mySignal();};
- public slots:
- void mySlot(){//my code};
- }
- IMyInterface& createMyImpl1(parent = 0)
- {
- return * new MyImpl1(parent);
- }
- IMyInterface& createMyImpl2(parent = 0)
- {
- return * new MyImpl2(parent);
- }
析构函数 前一定要加 virtual。
小结:关于Qt 编程点滴的介绍介绍完了,通过Qt 编程点滴介绍,也给自己提高了编程过程中需要注意的细节问题,由于本话题是一节一节为大家展现的,所以更多内容,请看编辑推荐。希望这些内容对你有所帮助!