QSQLTableModel inheritor and QTableView - qtableview

I wrote QSQLTableModel inheritor for working with qml and it's work well. I need use it with QTableView too, data shows, but I cannot modify it - when I edit everything is ok, but all changes drop when I get out from field (I know about editStrategy, but the problem occurs earlier). I suppose that something wrong with virtual function, but I cant undestant what. If i create QSqlTableModel with the same parameters, everything is ok. Somebody have any idea how can i fix this? My code:
h:
class ListModel : public QSqlTableModel
{
Q_OBJECT
Q_PROPERTY( int count READ rowCount() NOTIFY countChanged())
signals:
void countChanged();
public:
Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const;
ListModel(QObject *parent, QSqlDatabase _db):QSqlTableModel(parent,_db){this->setEditStrategy(QSqlTableModel::OnManualSubmit);}
void applyRoles();
#ifdef HAVE_QT5
virtual QHash<int, QByteArray> roleNames() const{return roles;}
#endif
private:
int count;
QHash<int, QByteArray> roles;
};
cpp:
//based on http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML
void ListModel::applyRoles()
{
roles.clear();
qDebug()<<"\n"<<this->tableName();
for (int i = 0; i < this->columnCount(); i++) {
QString role=this->headerData(i, Qt::Horizontal).toString();
roles[Qt::UserRole + i + 1] = QVariant(role).toByteArray();
qDebug()<<this->headerData(i, Qt::Horizontal);
}
#ifndef HAVE_QT5
setRoleNames(roles);
#endif
}
QVariant ListModel::data(const QModelIndex &index, int role) const{
QVariant value;
if(role < Qt::UserRole)
{
value = QSqlQueryModel::data(index, role);
}
else {
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
return value;
}
UPD
I understood that the problem is in data method's quantifier const, if I remove it everything is ok with QTableView, but I cant get data from model with gml's listviews. I see only one solution - replace interition from QSqlTableModel with aggregation it, but maybe someone knows better solution?

Summary: Solved with strange hack - inherited from QSqlRelationalTableModel instead QSqlTableModel, I think the reason is that QSqlRelationalTableModel has rewritten non virtual method data

Related

error: invalid new-expression of abstract class type ‘gr::zeromq::base_sink_impl’

namespace gr {
namespace zeromq {
class base_impl : public virtual gr::sync_block
{
public:
base_impl(int type, size_t itemsize, size_t vlen, int timeout, bool pass_tags);
virtual ~base_impl();
protected:
zmq::context_t *d_context;
zmq::socket_t *d_socket;
size_t d_vsize;
int d_timeout ;
bool d_pass_tags ;
};
class base_sink_impl : public base_impl
{
public:
base_sink_impl(int type, size_t itemsize, size_t vlen, char *address, int timeout, bool pass_tags, int hwm);
protected:
int send_message(const void *in_buf, const int in_nitems, const uint64_t in_offset);
};
} // namespace zeromq
}
`main(){
gr::zeromq::base_sink_impl *d_zeromq;
d_zeromq = new gr::zeromq::base_sink_impl(ZMQ_PUSH, sizeof(float), 1, "tcp://127.0.0.1:9098", 100, false, -1);
return 0;
}`
Hello!
After run this code, I have this error:
error: invalid new-expression of abstract class type ‘gr::zeromq::base_sink_impl’
_sink_impl(ZMQ_PUSH, sizeof(float), 1, "tcp://127.0.0.1:9098", 100, false, -1);
^
I'm not sure how to solve this error. I think I'm making wrong the constructor....
Please, any help will really appreciate it. Thanks
I find the error: the super class gr::sync_block has a virtual method called work.
I declared int work(int, gr_vector_const_void_star&, gr_vector_void_star&); inside the class base_impl and compiled.
Unfortunately, I can't run the project as It seems I have to configurate one of the CMakeList.txt to recognize zmq.hpp. It also seems I have to configurate the CMakeList.txt in my swig directory as I'm trying to do a GNURadio OOT Block using ZeroMQ.
Any help with it , I really Appreciate it.
Pd: If I resolve it, I 'll post it here:
GNU Radio OOT block : AttributeError: 'module' object has no attribute 'pthread' (using ZeroMQ sockets)

Seems scheduleUpdate not working

I'm trying to create a ScrollView class with cocos2d-2.0-rc2-x-2.0.1 and I mean to do something in update function to implement the auto-scroll effect.Unfortunately,I find the function has never been called.Though I've done a lot of work like searching on the internet,debuging step by step and so on,the possible solutions I found helped little.
As far as I know,my ScrollView class derive from CCNode and I've implemented the update function.The declaration of ScrollView is as following:
class ScrollView:public CCNode,public CCTouchDelegate
{
ClippingNode* visible_view;
CCNode* content_view;
//CCArray* items;
float row_margin;
float col_margin;
float interval_margin;
float last_y;//起始y方向坐标
float interval_dis;//间隔时间段内y方向上的位移。
bool touch_stopped;//标识触摸是否停止,主要用于自动滚动。
float up_bounder_y,down_bounder_y;//content_view的y方向坐标上下限
int items_num;
public:
static ScrollView* New(CCSize visible_view_size,float row_margin,float col_margin,float interval_margin,CCNode* background = NULL);
void ccTouchBegin(cocos2d::CCNode *node,const cocos2d::CCPoint &point);
void ccTouchMove(cocos2d::CCNode *node,const cocos2d::CCPoint &point);
void ccTouchEnd(cocos2d::CCNode *node,const cocos2d::CCPoint &point);
virtual void onEnter();
protected:
CCNode* makeCard();
void initContent();
private:
ScrollView():visible_view(NULL),content_view(NULL),touch_stopped(true){}
virtual ~ScrollView();
bool init(CCSize visible_view_size,float row_margin,float col_margin,float interval_margin,CCNode* background);
void update(float dt);
};
And here is the definition of update function:
void ScrollView::update(float dt)
{
CCLOG("update");
if(touch_stopped)
{
if(abs(interval_dis) < a)
{
interval_dis = 0.0f;
this->unscheduleUpdate();
}else
{
if(interval_dis < 0)
interval_dis += a;
else
interval_dis -= a;
const float future_y = content_view->getPositionY() + interval_dis;
if(future_y > down_bounder_y && future_y < up_bounder_y)
{
content_view->setPositionY(interval_dis);
}else if(future_y <= down_bounder_y)
{
content_view->setPositionY(down_bounder_y);
interval_dis = 0.0f;
}else
{
content_view->setPositionY(up_bounder_y);
interval_dis = 0.0f;
}
}
}
}
So I can ensure the type of the param is float instead of CCTime or ccTime which may cause update function never to be called.Moreover,I invoke the scheduleUpdate in the init method like the following:
bool ScrollView::init(CCSize visible_view_size,float row_margin,float col_margin,float interval_margin,CCNode* background)
{
visible_view = ClippingNode::New(visible_view_size);
CHECK_RETURN(visible_view,NULL,false);
visible_view->retain();
content_view = CCNode::create();//node函数中已调用autorelease
CHECK_RETURN(content_view,NULL,false);
content_view->retain();
this->row_margin = row_margin;
this->col_margin = col_margin;
this->interval_margin = interval_margin;
this->setAnchorPoint(ccp(0.5f,0.5f));
this->setContentSize(visible_view_size);
visible_view->setPosition(0,0);
content_view->setAnchorPoint(ccp(0,1));
content_view->setPosition(row_margin,visible_view_size.height);
content_view->setContentSize(CCSize(visible_view_size.width - 2 * row_margin,2 * col_margin));
this->addChild(visible_view);
visible_view->addChild(content_view);
down_bounder_y = visible_view_size.height;
up_bounder_y = content_view->getContentSize().height > visible_view_size.height?content_view->getContentSize().height:visible_view_size.height;
UserData* user_data = UserData::getUserData(this,true);
CHECK_RETURN(user_data,NULL,false);
user_data->setContainer(true);
items_num = 0;
initContent();
if(background)
{
background->setScaleX(visible_view_size.width/background->getContentSize().width);
background->setScaleY(visible_view_size.height/background->getContentSize().height);
background->setAnchorPoint(ccp(0.5f,0.5f));
background->setPosition(visible_view_size.width/2,visible_view_size.height/2);
user_data = UserData::getUserData(background,true);
user_data->setHitable(false);
this->addChild(background,-1);
}
this->scheduleUpdate();
return true;
}
Through debug,I can ensure the sentence "this->scheduleUpdate()" is invoked.In addition,I created a ScrollView object named scroll_view and added it to the main node through addChild function.So,where am I wrong?Any addvice would be appreciated and thanks for watching:p
I forgot to invoke the CCNode::onEnter in my own onEnter function. Thus all we need to do is invoke CCNode::onEnter in the ScrollView::onEnter. Hope other people don't make the mistake as I did.
If you invoke the onEnter, scheduleUpdate() may be not working.
CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(this,0,false);
or
CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(NewGame::update),this,0.1,false);
Dont know why your code is not working but have you tried this:
CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(cocos2d::CCObject *pTarget, int nPriority, bool bPaused);
you can check whether the node responds to update() call using:
pNode->getIsRunning();

Double linked list delete back node function

/* There is something wrong with the function delete_back(); I think something wrong with the remove function 3 parts.
Also remove_ele() I do not how to do it, thanks.
why I use the same method to delete element does not work
*/
#include <iostream>
using namespace std;
template<class T>
class doulinked
{
private:
doulinked *head;
doulinked *tail;
doulinked *prev;
doulinked *next;
T data;
public:
doulinked()
{
head=tail=prev=next=NULL;
T data;
}
void Inlist (doulinked *head);
void add(T d);
void insert_node();
void remove(doulinked* v);
void push_tail(T d);
void delete_front();
void delete_back();
void remove_ele (T d);
template <class U>
friend ostream & operator<<(ostream & os, const doulinked<U> & dll);
};
template<class U>
ostream & operator<<(ostream & os,const doulinked<U> & dll)
{
doulinked<U> * tmp = dll.head;
while (tmp)
{
os << tmp->data << " ";
tmp = tmp->next;
}
return os;
}
template<class T>
void doulinked<T>::add(T d)
{
doulinked *n = new doulinked;
n->data=d;
if( head == NULL)
{
head = n;
tail = n;
}
else
{
head->prev = n;
n->next = head;
head = n;
}
}
template<class T>
void doulinked<T>::push_tail(T d)
{
doulinked *n = new doulinked;
n->data=d;
if( tail == NULL)
{
head = n;
tail = n;
}
else
{
tail->next = n;
n->prev = tail;
tail = n;
}
}
template <class T>
void doulinked<T>::delete_front()
{
remove(head);
}
template <class T>
void doulinked<T>::delete_back()
{
remove(tail);
}
template <class T>
void doulinked<T>::remove(doulinked* v)
{
if(v->prev!=NULL && v->next!=NULL)
{
doulinked* p = v->prev;
doulinked* n = v->next;
p->next = n;
n->prev = p;
delete v;
}
else if(v->prev==NULL && v->next!=NULL)
{
doulinked* n =v->next;
head->next = n;
n->prev = head;
delete head;
head=n;
}
else if(v->prev!=NULL && v->next==NULL) // have some wrong with this loop;
{
doulinked* p=v->prev;
p->next=tail;
tail->prev=p;
delete tail;
tail=p;
}
}
template <class T>
void doulinked<T>::remove_ele(T d) // have some wrong with this loop
{
if(head->data==d)
{
remove(head);
head=head->next;
}
else
head=head->next;
}
int main()
{
doulinked<int> dll;
dll.add(5123);
dll.add(1227);
dll.add(127);
dll.push_tail(1235);
dll.push_tail(834);
dll.push_tail(1595);
dll.delete_front();
//dll.delete_back();
//dll.remove_ele(834);
cout<<dll<<endl;
system("pause");
}
Your design is a little confused.
The traditional C++ way to design a linked list (like std::list) has separate node and list classes, instead of a single class that acts as both:
template <typename T> struct node {
node *prev, *next;
};
template <typename T> struct list {
node *head, *tail;
};
If you want to just pass around node pointers, that's fine—but then you have to pass around node pointers, not node objects. And the mutator functions have to return a pointer as well—if you call delete_front on the head node, you've now got a reference to a deleted node; you need its next or you've lost any reference to the list. Since the constructor has to return a pointer, you can't use a real public constructor; you want a static factory method instead. And so on.
You also have to be consistent about whether there's a "sentinel node" before the head (and after the tail) or not. If you're creating a sentinel in your constructor—as you are doing—new nodes inserted at the end(s) need to point at the sentinel(s)—which you aren't doing.
Also, the whole head/tail notion you're using is wrong for a node API. (Also, it's incredibly confusing to mix and match names from different styles—you've got add matching delete_front and push_tail matching delete_back…) To have a push_tail method, you either have to walk the entire list (making it O(N)), or you have to have every node hold the tail pointer (making any list change O(N)), or you have to make the head hold a tail pointer and the tail hold a head pointer.
The last one works (it wastes a couple of pointers for every node when only one node needs each, but that rarely matters). But it gets confusing to think about.
It's actually a lot simpler to just create a circular list, where the head's prev points at the tail (or sentinel) instead of 0, and the tail's next points at the head (or sentinel) instead of 0. This gets you all the advantages of a separate list class, without needing that class—if you have a pointer to the head, that's all you need to refer to the entire list (because node is the head and node->prev is the tail, or or similarly if you have a sentinel).
Also, your constructor doesn't make much sense:
doulinked()
{
head=tail=prev=next=NULL;
T data;
}
This creates a local default-constructed T variable named data, and then… does nothing with it. You probably wanted to set data to something. And you probably wanted to use initializers for this. And in that case, you don't need to do anything, because that's already the default.
And I'm not sure what Inlist is even supposed to do.
As for remove_ele(T d), presumably you want to remove the first element whose data == d, right? If you write a find method first, then it's trivial: remove(find(d)). (I'm assuming that find throws an exception; if you want find to return null or the sentinel or something else instead, and remove_ele to return true or false, obviously you need one more line to check whether the find worked.)
If you don't know how to write a find method… well, that's kind of the whole point of a linked list, there's a trivial recursive definition for all traversal functions, including find:
node *node::find(T d) {
if (data == d) { return this; }
if (next) { return next->find(d); }
return 0;
}
Anyway, I think rather than try to bang on your code until it works, you should look at existing implementations of the various designs until you understand the differences, then pick the design you want and try to implement that.

C++ boost::bind and boost::function, class member function callbacks and operator==. What am I doing wrong?

I've got a problem with using boost::bind and boost::function and passing boost::function as a callback into another class.
Here's an example that is the problematic situation:
typedef boost::function<void (bool)> callbackFunction;
class HasCallback
{
public:
HasCallback() : value(0)
{
}
int value;
void CallBackFunction(bool changed)
{
std::cout << "HasCallback class. CallBackFunction called. Parameter: " << value << std::endl;
}
};
class ReceivesCallback
{
public:
void AddCallback(callbackFunction newFunc)
{
callbacks.push_back(newFunc);
}
void execute(int &i)
{
for(std::vector<callbackFunction>::iterator it = callbacks.begin(); it != callbacks.end(); it++)
{
(*it)(i++);
}
}
void RemoveHandler(callbackFunction oldFunc)
{
for(std::vector<callbackFunction>::iterator it = callbacks.begin(); it != callbacks.end(); it++)
{
if((*it) == oldFunc)
{
callbacks.erase(it);
break;
}
}
}
private:
std::vector<callbackFunction> callbacks;
};
int main()
{
HasCallback hc;
ReceivesCallback rc;
rc.AddCallback(boost::bind(&HasCallback::CallBackFunction, &hc, _1));
hc.value = 123;
HasCallback hc2;
rc.AddCallback(boost::bind(&HasCallback::CallBackFunction, &hc2, _1));
hc2.value = 321;
int a = 0;
rc.RemoveHandler(boost::bind(&HasCallback::CallBackFunction, &hc2, _1));
rc.execute(a);
}
The problem I'm having is that this doesn't even compile. It fails within ReceivesCallback::RemoveHandler in the if((*it) == oldFunc) line with the error saying that there's more than one overload of the operator== for the thing i'm trying to do.
I keep searching for this and can't find what I'm doing wrong. Also, I keep finding contradicting information, one saying that it's possible to compare boost::function-s and another saying it's not. I can see the operator== functions within boost/function_base.hpp and i believe this is supposed to work, I just can't seem to figure out how. Can someone help me out here? My suspicion is that it fails because the parameters of the boost::bind need to be specified fully(be concrete values) but this is something i cannot get in the code I'm developing, I just need to know whether the passed handler is registered or not, since I'm binding to an object it should have all the information neeeded to make the distinction.
See Boost.Function FAQ for an explanation : Why can't I compare boost::function objects with operator== or operator!= ?.
Boost.Functions only provides comparison of a boost::function with an arbitrary function object. I believe that making your RemoveHandler member function template could fix the issue :
template<class Functor>
void RemoveHandler(const Functor &oldFunc)
{
for(std::vector<callbackFunction>::iterator it = callbacks.begin(); it != callbacks.end(); it++)
{
if((*it) == oldFunc)
{
callbacks.erase(it);
break;
}
}
}
Here, oldFunc gets to keep its actual type without being 'wrapped' in a boost::function.

Custom STL Containers

I have written code that allows one to traverse mapped data in the order it was entered.
The solution I coded a couple of times was:
Given a keytype, K, and and data type, D,
std::map
std::vector
When one wanted to randomly find a data entry, use map.find(K). When one wanted to traverse the map in entry order, use std::vector::iterator (begin(), end()].
This was fine, but as an exercise, I wanted to write this 'OrderedMap' as an STL compliant container. I also have (stripped down to this discussion):
template <typename K, typename D>
class OrderedMapValue
{
private:
K first_ref;
std::map<K,size_t>& m;
std::vector<D>& v;
public:
const K& first
D& second
assignment operator=(const D& data)
{
std::map<K,size_t>::const_iterator iter = m.find(first_ref);
v[iter.second] = data; // error checking of iter stripped
}
};
Further assuming
template <typename K, typename D>
class OrderedMap
{
public:
typename OrderedMapValue<K,D>& OrderedMap<K,D>::operator[](const K&);
// snip...
};
class MyClass
{
public:
MyClass(std::string s) : _my_data(s) {}
private:
std::string _my_data;
};
The following code works:
OrderedMap<std::string,MyClass*> omap;
omap["MyKey"] = new MyClass("dummy");
However, this code does not:
OrderedMap::iterator iter = omap.find("MyKey");
MyClass * obj = iter->second;
delete obj;
iter->second = new MyClass("dummy");
Assuming I have done something
a) Structurally silly or
b) Unnecessarily complex, how should this be done?
I realize that I'm likely reinventing the wheel here, but again, this effort is mainly to increase my knowledge of STL containers, their design patterns and proper use.
Thanks in advance for any insights,
I don't have a compiler right now to test this, so there could be errors, but I think you want it more like:
template <typename K, typename D>
class OrderedMap
{
private:
std::map<K,size_t> &m;
std::vector<D> &v;
public:
typename pair<K,D> TYPE;
TYPE& operator[](const K &k)
{
return v[ m[ k ]];
}
TYPE& operator[](size_t idx)
{
return v[ idx ];
}
pair<iterator,bool> insert( const TYPE& pair )
{
map<K, size_t>::const_iterator iter;
iter = m.find( pair.first );
if( iter != m.end() )
return make_pair( v[ iter.second], false );
m.insert( make_pair( pair->first, v.size() ));
v.push_back( pair->second );
return make_pair( v.last() , inserted );
}
iterator &begin()
{
return v.begin();
}
// etc
};
In OrderedMapValue::operator=, you have:
std::map<K,size_t>::const_iterator iter = m.find(first_ref);
What is first_ref? The code doesn't reference it (no pun intended) elsewhere. It looks to me like it might be a vestige from an older implementation, replaced elsewhere by the public member
const K& first.
Could this be the problem?
EDIT from the comments: The code doesn't show that first_ref is initialized anywhere; so for all I can tell, the call to m.find(first_ref) is searching for an empty string, rather than the key for the OrderedMapValue.