opening a QTableView window with a pushbutton
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:
Code:
int main(int argc, char *argv[])
{
//some splash screen stuff
dcsTS w;
w.show();
//more splash screen stuff
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:
Code:
void dcsTS::showTable(){
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?
Re: opening a QTableView window with a pushbutton
Code:
void dcsTS::showTable(){
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:
Code:
class dcsTS ... {
...
protected:
RegModel * _model;
...
// dcsTS constructor:
dcsTS::dcsTS( /*...*/ ){
_model = new RegModel(0);
_view->setModel(_model);
}
//
void dcsTS::showTable(){
_view->show();
}
Re: opening a QTableView window with a pushbutton
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:
is ambiguous between one of these two declarations in the header file:
Code:
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".
Re: opening a QTableView window with a pushbutton
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.
Re: opening a QTableView window with a pushbutton
awesome, after solving some of my own little errors it worked out perfectly, thank you!