Active Topics

 


Reply
Thread Tools
Posts: 341 | Thanked: 57 times | Joined on Nov 2009
#1
This first issue has been solved (my silly mistake!):

Hey guys .. Well ever since I've started programming in Qt, I've been having problems regarding scope visibility of objects I've defined .. Till now I've managed to find ways to get around these things but now its getting annoying ..

For example, I have this class defined called Canvas:

Code:
class Canvas : public QWidget//, public MainWindow
{
    Q_OBJECT
public:
    void refreshFoldersList(QString inputPath);
    void browseFolders();

private:
    QListWidget *foldersList;

};

#endif // CANVAS_H

Now even though foldersList is private, refreshFoldersList() should be able to 'see' it, right ? But in my case it can't .. ! I first initialize foldersList in the browseFolders() function and then from within browseFolders(), I call refreshFoldersList() ... Any code inside refreshFoldersList() dealing with foldersList immediately throws a segmentation fault ... I've checked the pointer values for foldersList when the scope is in browseFolders() and refreshFoldersList() .. the values don't match .. its like either I'm trying to access something I'm not supposed to, or that I'm trying to access an object which has not been initialized yet ..

Any clues on this ?

Second problem is still an ongoing issue:

A related issue ... I have another class MainWindow (inherited from QMainWindow) .. In this class I have an instance of the class Canvas .. this instance is named canvas .. I initialize canvas in MainWindow's constructor, and set canvas's parent to the MainWindow instance initializing it .. based on this, I used the following code to access a MainWindow function from within the Canvas class:

Code:
((MainWindow*)parent())->someFunctionDefinedInMainWindow();
Before, the above code used to work .. but then like 2-3 days ago it stopped working all of a sudden .. Now it got me to inside the MainWindow function I was calling (namely someFunctionDefinedInMainWindow() ), but from there if I tried to access any variable defined in MainWindow, I got a Segmentation Fault, again with pointer values not matching .. The way I solved this is by defining a variable as:

Code:
void * papa;
.. inside Canvas, and then when I initialized canvas, I set:

Code:
canvas->papa = this; //this being the MainWindow instance initializing canvas
Now I accessed MainWindow's functions like this:

Code:
((MainWindow*)papa)->someFunctionDefinedInMainWindow();
.. which works!

But again, I would like to know the nature of these problems .. Am I doing something wrong or what ?

Last edited by ahmadka; 2010-07-05 at 14:59.
 
Posts: 353 | Thanked: 263 times | Joined on Dec 2009 @ Finland
#2
How about changing all the "-->" to "->"? I don't think there is a operator -->
__________________
My Maemo5 projects:
mSpede - Speed testing game | Them Bloody Ducks - 2D duck hunting game | Maetronome - A simple metronome app | CuteMPC - MPD client
 
Posts: 341 | Thanked: 57 times | Joined on Nov 2009
#3
Originally Posted by TNiga View Post
How about changing all the "-->" to "->"? I don't think there is a operator -->
No I'm using -> in my code everywhere .. --> mentioned above was typed up manually, hence the error :P .. I'll correct that now ..
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#4
Scope is a semantic property, getting a segmentation fault when accessing an attribute is something else.

Please supply the code where you "initialize foldersList in the browseFolders() function". Most likely you're not allocating memory in the way you're expecting to.

Also, make sure you're using the same object instance when initializing and referencing.
 
Posts: 341 | Thanked: 57 times | Joined on Nov 2009
#5
Alright, here's the refreshFoldersList() function

Code:
void Canvas::refreshFoldersList(QString inputPath)
{
    QDir *dir = new QDir(inputPath);
    dir->setFilter(QDir::AllDirs);
    dir->setSorting(QDir::Name);

    if(dir->exists()==true)
    {
        foldersList->clear(); //problem
        foldersStringList = new QStringList(dir->entryList());
        foldersStringList->removeFirst();
        foldersStringList->removeFirst();
        foldersList->addItems(*foldersStringList); //Problem

    }
    else
    {
        printMessage("ERROR f9g87: Path doesn't exist!!");
    }

}
And here's the browseFolders() function (called first!):

Code:
void Canvas::browseFolders()
{

    QListWidget *foldersList = new QListWidget();
    foldersList->setSelectionMode(QAbstractItemView::SingleSelection);

    
    directoryUpButton = new QPushButton("Up");
    newFolderButton = new QPushButton("New");
    selectCurrentDirectoryButton = new QMaemo5ValueButton("Shams");

    refreshFoldersList("/home/user/MyDocs/"); //THE CALL IS MADE

    overallLayout = new QHBoxLayout();
    spbl = new QVBoxLayout();

    spbl->addWidget(directoryUpButton);
    spbl->addWidget(newFolderButton);
    spbl->addWidget(selectCurrentDirectoryButton);
    overallLayout->addWidget(foldersList);
    overallLayout->addLayout(spbl);

    savePaintingFolderDialog = new QDialog();
    savePaintingFolderDialog->setLayout(overallLayout);
    savePaintingFolderDialog->show();

}
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#6
You define an attribute named foldersList and then you're surprised that things go wrong when you do
Code:
QListWidget *foldersList = new QListWidget();
?

foldersList will get a new value every time and it will be a local variable and not the object attribute that gets updated.
 

The Following User Says Thank You to Joorin For This Useful Post:
Posts: 3,617 | Thanked: 2,412 times | Joined on Nov 2009 @ Cambridge, UK
#7
Originally Posted by ahmadka View Post
Alright, here's the refreshFoldersList() function

Code:
void Canvas::refreshFoldersList(QString inputPath)
{
    QDir *dir = new QDir(inputPath);
    dir->setFilter(QDir::AllDirs);
    dir->setSorting(QDir::Name);

    if(dir->exists()==true)
    {
        foldersList->clear(); //problem
        foldersStringList = new QStringList(dir->entryList());
        foldersStringList->removeFirst();
        foldersStringList->removeFirst();
        foldersList->addItems(*foldersStringList); //Problem

    }
    else
    {
        printMessage("ERROR f9g87: Path doesn't exist!!");
    }

}
And here's the browseFolders() function (called first!):

Code:
void Canvas::browseFolders()
{

    QListWidget *foldersList = new QListWidget();
    foldersList->setSelectionMode(QAbstractItemView::SingleSelection);

    
    directoryUpButton = new QPushButton("Up");
    newFolderButton = new QPushButton("New");
    selectCurrentDirectoryButton = new QMaemo5ValueButton("Shams");

    refreshFoldersList("/home/user/MyDocs/"); //THE CALL IS MADE

    overallLayout = new QHBoxLayout();
    spbl = new QVBoxLayout();

    spbl->addWidget(directoryUpButton);
    spbl->addWidget(newFolderButton);
    spbl->addWidget(selectCurrentDirectoryButton);
    overallLayout->addWidget(foldersList);
    overallLayout->addLayout(spbl);

    savePaintingFolderDialog = new QDialog();
    savePaintingFolderDialog->setLayout(overallLayout);
    savePaintingFolderDialog->show();

}
You're creating foldersList as a local variable in browseFolders. You should just be able to do "foldersList = new QListWidget();" (or maybe "this->foldersList = new QListWidget();" - I've not worked with C++ in quite a while). You're also passing foldersList to the refreshFoldersList function, though it doesn't accept it - I doubt that's connected but it could cause issues elsewehere.
 

The Following User Says Thank You to Rob1n For This Useful Post:
Posts: 13 | Thanked: 12 times | Joined on Apr 2010 @ Finland
#8
problem: QListWidget *foldersList = new QListWidget();
you define new QListWidget pointer in browseFolders() and it cant be seen outside
correct: foldersList = new QListWidget();
 

The Following User Says Thank You to tommiasp For This Useful Post:
Posts: 341 | Thanked: 57 times | Joined on Nov 2009
#9
wow, lol .. am I embarassed or what !? .. I guess I didn't get enough sleep last night

Thanks for pointing out my silly mistake ..

But the second issue I mentioned in OP still is a mystery for me .. Although I've found a workaround but still, any insight into that would be really helpful too ..
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#10
Again: Talking about code is pretty useless in this context. Please supply the code where everything you describe is done.

Saying that you set something as the parent of something else is good information but code is king.
 
Reply


 
Forum Jump


All times are GMT. The time now is 21:02.