Demo2

来源:互联网 发布:普利茅斯大学知乎 编辑:程序博客网 时间:2024/05/16 19:31

关于 QComboBox 终极解决办法:
用了n中方法在tableWidget中显示combox,一直出现各种问题(上一篇博客)。
这次使用了一个比较暴力的办法: 每点击一个cell就生成一combox,用完之后就删除。
这个方法一开始就想到了,问题在于用delete 就程序崩溃了。
意外发现 QComBobox 有方法:void QObject::deleteLater()
可以拿来用。

文档上对此方法的解释如下:

Schedules this object for deletion.
The object will be deleted when control returns to the event loop. If the event loop is not running when this function is called (e.g. deleteLater() is called on an object before QCoreApplication::exec()), the object will be deleted once the event loop is started. If deleteLater() is called after the main event loop has stopped, the object will not be deleted. Since Qt 4.8, if deleteLater() is called on an object that lives in a thread with no running event loop, the object will be destroyed when the thread finishes.
Note that entering and leaving a new event loop (e.g., by opening a modal dialog) will not perform the deferred deletion; for the object to be deleted, the control must return to the event loop from which deleteLater() was called.
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.

感觉是这样的:
因为对象可能在事件循环中存在,不能直接删; 而是需要往消息循环中投递一个消息安全的删除,不然假如还有针对那对象的消息而对象被销毁了的话程序就崩溃了。因此就使用deleteLater()。

参考博客:
http://blog.csdn.net/dbzhang800/article/details/6300025

修改之后的代码:

void Widget::doubleClick(int row,int col){    setHighlight(tableWidget->currentRow(),tableWidget->currentColumn());    combox = drawComboBox();    currentcombox = combox;    tableWidget->setCellWidget(row,col,combox);    QObject::connect(combox,SIGNAL(currentIndexChanged(int)),this,SLOT(handleCombox(int)));    QObject::connect(tableWidget,SIGNAL(itemSelectionChanged()),this,SLOT(click()));}void Widget::click(){    if(NULL != combox){        combox->deleteLater();        combox = NULL;    }}

这样就可以随便双击,单击,选定了。

关于 增加移除附件操作的说明

增加类:

class AccessoryType{public:    AccessoryType(int pos = -1);    int posEllpse;    int posRect;    int posU;};

用来保存个中附件的最后操作的位置。根据位置判断各种操作的合法性,进而使得菜单的非法操作Disabled。

方法1:
在Widget中添加对象数组:

    AccessoryType addType[34];    AccessoryType removeType[34];

使用对象数组保存每颗牙齿每个附件的每种操作的最后位置,
该做法只需在增加删除附件时记录一下位置即可。无需遍历牙齿个各个阶段。
缺陷在于:
1.内存空间的浪费。
2.由于只保存了附件操作最后一次的位置,因此在最后一次的位置上进行附件操作则无法保证正确的合法性判断出错。

方法 2:
在右击显示菜单时,遍历这颗牙齿各个阶段的操作,进而确定附件操作的最后位置。
问题在于:对同一颗牙齿进行操作需要多次遍历。
但是可以保证正确的合法性判断。

写完方法一后,发现在加附件操作所在阶段进行更改会出错。因此使用方法二应该会更好。

0 0
原创粉丝点击