PDA

View Full Version : opening a QTableView window with a pushbutton



willief
7th April 2011, 19:00
Hi, I'm having some trouble trying to show a QTableView window with a push button.
When I have the following in my main.cpp, it opens both the main window and the table window:


int main(int argc, char *argv[])
{
QApplication a(argc,argv);
//some splash screen stuff
dcsTS w;
w.show();
//more splash screen stuff

QTableView tableView;
RegModel RegModel(0);
tableView.setModel(&RegModel);
tableView.show();

return a.exec();


Now, I created a function that in my main window file that activates a pushbutton, that when clicked should run those same four lines of code:


void dcsTS::showTable(){
QTableView tableView;
RegModel RegModel(0);
tableView.setModel(&RegModel);
tableView.show();
}

instead of having it in the main file. I was hoping this would magically make the table pop up when i pressed the button, however this is not the case, any ideas?

stampede
7th April 2011, 19:29
void dcsTS::showTable(){
QTableView tableView;
RegModel RegModel(0);
tableView.setModel(&RegModel);
tableView.show();
}// ... tableView and RegModel destroyed here

Your variables goes out of scope, create them on a heap ( operator new ) and / or keep as class members. Also, don't use class name as variable name, it's very confusing:


class dcsTS ... {
...
protected:
QTableView * _view;
RegModel * _model;
...

// dcsTS constructor:
dcsTS::dcsTS( /*...*/ ){
_view = new QTableView();
_model = new RegModel(0);
_view->setModel(_model);
}

//
void dcsTS::showTable(){
_view->show();
}

willief
7th April 2011, 20:07
Glorious, I understand now and believe this is going to work, but there is one more small issue.
When I declare the new constructor in the .h file, when I try to compile I get an error in my main file telling me that this call:


dcsTS w;


is ambiguous between one of these two declarations in the header file:


public:
explicit dcsTS(QWidget *parent = 0);
dcsTS();
//there is also a destructor here


any ideas about how to get around this? thanks for all your help :D

EDIT: the actual error says "error: call of overloaded 'dcsTS()' is ambigious".

stampede
7th April 2011, 20:14
Don't add new constructor, put the code I've posted into existing one.
About the error - it's because you have default value declared in first constructor, so compiler does not know what do you mean, should it call first one with parent=NULL, or second.
Anyway, you don't need two constructors, just modify previous.

willief
7th April 2011, 20:57
awesome, after solving some of my own little errors it worked out perfectly, thank you!