-1

I am new to Qt and wanted to execute something if my treeWidget is empty.

I tried topLevelItemCount, but that didnt work;

if (treeWidget_2->topLevelItemCount() == 0) {
     //this doesn't even get executed.
}

I get a segfault when doing that. I'm completely sure its because of toplevelCount, because I went through it with the debugger.

BTW: My TreeWidget isn't empty. It has one item and this has one subitem

treeWidet
    |-----test
            |---test

I also followed this tutorial and created a function treeCount:

int MainWindow::treeCount(QTreeWidget *tree, QTreeWidgetItem *parent = 0) //counts the nodes inside a specific tree
{
    int count = 0;
    if (parent == 0) {
        int topCount = tree->topLevelItemCount();
        for (int i = 0; i < topCount; i++) {
            QTreeWidgetItem *item = tree->topLevelItem(i);
        }
        count += topCount;
    } else {
        int childCount = parent->childCount();
        for (int i = 0; i < childCount; i++) {
            QTreeWidgetItem *item = parent->child(i);
        }
        count += childCount;
    }
    return count;
}

I changed it a little bit to fit my needs. No problem there. The real struggle is this error message:

default argument given for parameter 3 of 'int blablabla' [-fpremissive]

int MainWindow::treeCount(QTreeWidget *tree, QTreeWidgetItem *parent = 0)
                                                                       ^

I'm using Qt 5.7 on Linux. (GNOME, if that matters)

As I am a total newb to Qt, I might have forgotten something important. feel free to ask for it ;)

Community
  • 1
  • 1
  • 1
    Please revise your question... `treeWidget_2->topLevelItemCount == 0` is not a function call of `toplevelItemCount` and you specify default function arguments in function declarations, not definitions. Come back with a MCVE. – LogicStuff Mar 04 '17 at 10:34
  • sorry, forgot the bracket while typing it. It is in my code @LogicStuff – ThefrenchSpeedruns Mar 04 '17 at 10:37
  • In addition to what @LogicStuff said, I'd guess that `treeWIdget_2` is null during your call, which causes the segfault. Use a debugger or a null check to verify and solve it! – batbrat Mar 04 '17 at 10:37
  • @batbrat yeah, the treeWidge_2 is null. (I checked with if (treeWidget_2 == NULL)) How can I change that? – ThefrenchSpeedruns Mar 04 '17 at 10:40
  • Well, there's not enough information to say; I'd suggest tracing the call back and verifying that the widget is actually created, and that the pointer is actually assigned by the point you want to use it. Good luck! – batbrat Mar 04 '17 at 10:43
  • ok, Thank you ;) @batbrat actually, I created everything in Qt Creator, so I suppose, it shouldn't be NULL, shouldn't it? – ThefrenchSpeedruns Mar 04 '17 at 10:53
  • @ThefrenchSpeedruns, simply creating it in Qt creator doesn't protect you, as there may be other coding issues. One example is that the widget creation may happen at a specific point in the applications life-cycle, but we have tried to use a pointer to a widget at some point before that creation. – batbrat Mar 04 '17 at 14:40
  • Well. This event happens on buttonclick, so theoretically the treeWidget should already exist @batbrat – ThefrenchSpeedruns Mar 04 '17 at 14:46
  • OK: I uploaded my code to github: https://github.com/MaximilianJugendForscht/PhiluminaEngine_Test/tree/master/gameengine – ThefrenchSpeedruns Mar 04 '17 at 15:30
  • @batbrat (just to mention u) – ThefrenchSpeedruns Mar 04 '17 at 15:30

1 Answers1

0

I read through the code presented, and noticed a few problems:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

Here, you you should be doing: ui(new Ui::MainWindow). The basic idea is that the class exists, but is not constructed and ready for use; that's why your QTreeWidget is null. For a more detailed explanation on the difference between the two calls, please look at this Stackoverflow question.

Now, I am unclear on why you have a separate pointer to the object that you new this way. As I understand it, the this pointer will serve your needs: there is no need to create the MainWindow object using new and store it at all! If you do this, you don't need to explicitly delete the object in the destructor either. Please refer to this Stackoverflow Question for an explanation on delete this;, in case you think that you wish to explicitly delete. The short answer in your code is, "No". You shouldn't call delete this in the destructor.

I also noticed the following issues:

  • explicit MainWindow(QWidget *parent = 0); It doesn't seem like a good idea to set a default value of 0 in this case. I recommend removing it unless you have a very good reason to keep it.
  • The code overall is a bit confusing because function names do not reflect their purpose very well, at least to me. I would suggest re-naming some of your functions to make the code more readable. For e.g., I expect newNode() to have something to do with creating a new node, but it actually calls collapseAll() :-

    void MainWindow::newNode() { 
        ui->treeWidget->collapseAll();
    }
    
  • It looks like you're taking on quite a big project, but are learning the components required to build it all at once. I've been there before, and, from my experience, I strongly recommend getting a solid foundation in the basics before proceeding. A few weeks/months of up-front effort equals huge time savings down the line.

Do try this out and let me know. I hope this helps.

Community
  • 1
  • 1
batbrat
  • 5,155
  • 3
  • 32
  • 38