dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

qt5 creator: added a QTableView but no grids to see

Mon May 17, 2021 6:03 pm

again qt5 creator:
I added a QTableView to my form by drag+drop and resized it, but no cell grids to see when I compiled + run.

in the QTableView context menu the preferences are:

Code: Select all

enabled     X (checked)
geometry   [20,310), 670x490]
X           20
Y           310
width      670
height     420

base size:  
width      10
height     10

autoscroll  X (checked)
...
showGrid    X (checked)
nonetheless, just a blank white canvas, no structure, no bars, no lines, no grids...

what's faulty or missing?

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Mon May 17, 2021 10:24 pm

You're going to have to add more information than that, a code snippet would be helpful.

You've added a model to it?
You've set up the header for it?

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Tue May 18, 2021 8:21 am

I have added it just by drag and drop from the designer tools menu like I add a Button or a label or a QGraphicsScene.
Except for a couple of variable declararations I also didn't have to write function code by myself ( main.cpp, mainwindow.h, and mainwindow.ui are done automatically by the qt creator GUI ).

(edit, updated)
I meanwhile also created an event slot action for compile-testing though ( for mouse click ), my tableView object now renamed to "table"..

These are my project files: https://github.com/dsyleixa/RaspberryPi ... oL_NAND_v3
and this is a GUI IDE screenshot: https://github.com/dsyleixa/RaspberryPi ... nshot.jpeg

as stated, ftm I just expected an empty table with grids and 10x10 empty fields like in an empty Excel table.

Or do I have to use a "table widget" (item-based) instead of a "table view" (model-based)?

I want to use the table grids/fields to show up the Game Of Life dots (and later, in a 2nd step, also modify some cells to true (black) or false (white) by mouse clicks ),
similar like in this original example (which I faild to make it run): https://doc.qt.io/qt-5.12/qtquick-table ... ample.html .

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Tue May 18, 2021 11:15 am

You are making this way more complicated than it needs to be, QTableView or QTableWIdget is overkill and is complicating things.

Create a widget that will contain the "game board".

Create a QGridLayout instance and assign that as the container widgets layout.

Now you can put in that grid at any x/y position a child widget, if you use QLabels then you can set the min & max sizes to something appropriate, let's say 16px/16px and you can do something like:

lifeWidget->setStylesheet("background-color: red")

Not the most efficient way, but it will work.

The other alternative is to override paintEvent on a QWidget subclass and then paint the life board in the widget, that is probably the best and simplest answer, it's literally just 2 for loops looping over x & y and reading the game board from an array to decide which colour a particular grid square should be.

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Tue May 18, 2021 11:42 am

I'm afraid I can't find a GameBoardWidget to d+d from my widget tool menu. Can you give me a hint where it's located?
OTOH, for drawing the updated display fields I need an overall scalable table-like field numbering to repeatedly assing my GoL board array to the display table values

Code: Select all

char board[3000][3000]; // GoL board array // perhaps even bigger

for int y=0; y<3000; y++)  {
   for int x=0; x<3000; x++) {
      ui->table[y][x]= board[y][x];
   }
}
I'm not clear about how to make that by 9 million single labels (inclusively scrolling an zooming), tbh...

I chose the TableView because this widget is used by the Qt GoL example program (which I can't compile though because of a missing module), but I agree that in this example everything is an incredibly muddled hotchpotch by it's codings...

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Tue May 18, 2021 1:31 pm

dsyleixa123 wrote:
Tue May 18, 2021 11:42 am
I'm afraid I can't find a GameBoardWidget to d+d from my widget tool menu. Can you give me a hint where it's located?
OTOH, for drawing the updated display fields I need an overall scalable table-like field numbering to repeatedly assing my GoL board array to the display table values

Code: Select all

char board[3000][3000]; // GoL board array // perhaps even bigger

for int y=0; y<3000; y++)  {
   for int x=0; x<3000; x++) {
      ui->table[y][x]= board[y][x];
   }
}
I'm not clear about how to make that by 9 million single labels (inclusively scrolling an zooming), tbh...

I chose the TableView because this widget is used by the Qt GoL example program (which I can't compile though because of a missing module), but I agree that in this example everything is an incredibly muddled hotchpotch by it's codings...
If you're wanting that many cells then creating that many widgets is not an option and a table isn't appropriate either.

You won't find a game board widget because there isn't one, you need to implement it yourself.

Either use QGraphicsView or subclass QWidget and set its dimensions appropriately to the size of the board, you paint the board yourself in paintEvent using a QPainter.

You should put that widget in a QScrollArea and then you will be able to scroll about your widget.

You will have to implement scaling yourself.

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Tue May 18, 2021 1:58 pm

well, I have already created the screen by QGraphics, but I cannot easily edit a single cell by mouseclicks:
https://github.com/dsyleixa/RaspberryPi ... nshot.jpeg

So my idea was to transfer the Graphics scene to a table which's cells can be zoomed and even edited manually (e.g., by mouse doubleclick) during runtime.

OTOH, QTableView provides up to about 16Million x 16Million cells (IIUC), that would fit IMO

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Tue May 18, 2021 3:32 pm

dsyleixa123 wrote:
Tue May 18, 2021 1:58 pm
well, I have already created the screen by QGraphics, but I cannot easily edit a single cell by mouseclicks:
https://github.com/dsyleixa/RaspberryPi ... nshot.jpeg

So my idea was to transfer the Graphics scene to a table which's cells can be zoomed and even edited manually (e.g., by mouse doubleclick) during runtime.

OTOH, QTableView provides up to about 16Million x 16Million cells (IIUC), that would fit IMO
QTableView is not designed for large datasets out of the box, you will have to do considerable amount of effort to make it behave, each one of those entries in the table will have an allocated object, you're heading for a world of pain with a large dataset.

With the datasets you're suggesting you want to use, you are pretty much going to want to make it as optimised as possible, as I said before, you need to:

subclass QWidget as MyBoardWidget

override paintEvent and draw the boxes that are currently visible in the widgets viewport, when you embed the QWisget in a QScrollArea you will get told the visible rectangle, you will then only draw what's absolutely necessary (you will need to provide this logic yourself).

You'll also need to provide the logic for zooming, but that's trivial.

You may get away with a QGraphicsView, my project here:

https://github.com/nedrysoft/dmgee/blob ... apItem.cpp

Has a QGraphcisItem subclass that responds to clicks, drags and so on. I'm not sure why you think that a QGraphcisView or QGraphicsItem is not easy to interact with via the mouse, it most definitely is, it's designed for that very task.

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Tue May 18, 2021 4:31 pm

thank you, but I have no plan or idea how to write a sublass, how to override paintEvent , how to draw "the" boxes, what a widgets viewport is, how to embed a QWidget in a QScrollArea, and all that, so I won't get along with my QGraphicsVew: I don't talk oop-ish admittedly - all I can do is drag+drop widgets into my form and then adjust preferences and settings (and that is why I actually use just the qt creator GUI IDE)...
If you say that the tableView can not be achieved to work in simple steps, then surely I will believe it and bury that idea again (I thought it must be a good way because also the Qt developers used it in their examples, but anyway...)
What I actually needed would be a simple and light-weight table system with sizeable and interactive fields/grids to work out of the box, but that appearently doesn't seem to exist either.

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Tue May 18, 2021 6:56 pm

dsyleixa123 wrote:
Tue May 18, 2021 4:31 pm
thank you, but I have no plan or idea how to write a sublass, how to override paintEvent , how to draw "the" boxes, what a widgets viewport is, how to embed a QWidget in a QScrollArea, and all that, so I won't get along with my QGraphicsVew: I don't talk oop-ish admittedly - all I can do is drag+drop widgets into my form and then adjust preferences and settings (and that is why I actually use just the qt creator GUI IDE)...
If you say that the tableView can not be achieved to work in simple steps, then surely I will believe it and bury that idea again (I thought it must be a good way because also the Qt developers used it in their examples, but anyway...)
What I actually needed would be a simple and light-weight table system with sizeable and interactive fields/grids to work out of the box, but that appearently doesn't seem to exist either.
There is a game of life QML example using a table view, but it's tiny in comparison (with regards to the number of cells) to what you're suggesting that you want to do.

This comes back to the other thread when I said that C++ is not a language that somebody should jump into learning programming with, there are much more suitable languages, there are so many things that can potentially trap you along the way, that said, you might want to look at QML, it's a declarative language that is ideal for the type of application you're trying to write.

For the most part, you can construct your UI in a simple manner (although it's generally described by hand rather than through a GUI) and you can write the core logic in C++.

You're still going to have to learn about subclassing, inheritance and Qt idioms like signals and slots, there's no way of getting around that with Qt.

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Wed May 19, 2021 7:22 am

well, my actual program codes are always written in procedural C, extended by some C++ classes - I am used to it since I am programming with the Arduino IDE (even using a touchscreen GUI ) and also once programming with the Borland C++ Builder for my Windows PCs and for some robot interfaces, and generally I'm fine with it.
Also most of my Raspi programs work that way although usually the Pi lacks suitable C(++) driver libraries (whilst tons of them are available in C++ Arduino code, and the Arduino people are impressively demonstrating what all is possible by user-friendly C++ code libs which even I can use).
OTOH, the qt GUI IDE is needed only for the look and for some GUI controls, which show up just visual interfaces to my actual code and mostly not computing new things which I haven't coded before in common C procedures - but just for an "appearance" I wouldn't be ready for the effort to learn an extra PL, and qml, json, javascript, or Python certainly not tbh. (I used partially Java for some robot interfaces, and Python for some small minor projects, but to my experiences those messy Python or java codings are an incredible gibberish... :roll: )
So no, plain C and C++ with convenient user-friendly Arduino-like libs is the only option, and that's what I want to achieve with the qt creator and just by it's graphical form designer tools.

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Wed May 19, 2021 10:00 am

but back to topic, out of interest, based on my code and my preferences/settings (I linked to above):
why doesn't the table grid show up already when I compile an run my program and what do I have to change or add to fix that?

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Wed May 19, 2021 11:38 am

dsyleixa123 wrote:
Wed May 19, 2021 10:00 am
but back to topic, out of interest, based on my code and my preferences/settings (I linked to above):
why doesn't the table grid show up already when I compile an run my program and what do I have to change or add to fix that?
Because all you've done is instantiate the view, you haven't provided a model. It's "empty" because it's empty.

You're going to have a lot of duplicated data because you're storing your "board" in an array, with the table view you won't need to that, the model is the array. A model item will allow you to store arbitrary data in an item using the abstract method ->setData(index, data), data is a QVariant type and you can use Qt's meta-type system to store arbitrary data, you can create as many data items per item as you need, use the indices UserRole+n to do this.

You'll probably want to provide a QItemDelegate (or QSytledItemDelegate) as well so that the cell is painted how you want it, you can set a delegate on a column or whole table basis.

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Wed May 19, 2021 12:02 pm

ok, as to the duplicated data I understand, but I want to do all the basic computations by plain C in my 2 arrays (board and tmpboard), whilst the qt table is mainly just to display. I'm using the basic C computations by those arrays also on my Arduinos, so it's easy to port and to maintain.
But what is a "model" and what is that for? Finally for my GQraphicView scene I also have just to instanciate the scene and then I can draw to it, lacking any "model", and buttons and labels I do not even have to instanciate for captions and for button event actions.
In my table I 1st of all just wanted to show up empty fields with border lines (grids).
So what will my actual code have to be for that purpose?

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Wed May 19, 2021 1:16 pm

dsyleixa123 wrote:
Wed May 19, 2021 12:02 pm
ok, as to the duplicated data I understand, but I want to do all the basic computations by plain C in my 2 arrays (board and tmpboard), whilst the qt table is mainly just to display. I'm using the basic C computations by those arrays also on my Arduinos, so it's easy to port and to maintain.
But what is a "model" and what is that for? Finally for my GQraphicView scene I also have just to instanciate the scene and then I can draw to it, lacking any "model", and buttons and labels I do not even have to instanciate for captions and for button event actions.
In my table I 1st of all just wanted to show up empty fields with border lines (grids).
So what will my actual code have to be for that purpose?
A model is the data, this is a basic paradigm in programming no matter what language you use, you will need to read about MV, MVC and all the other ways that you deal with data that is to be viewed.

The graphics view does have a model, a graphics scene. The scene contains the information about the graphic objects and provides an *optimised* list of objects to the view when the view wants to refresh its view, you can use the built-in scene as-is or you can implement your own if you think you can do a better job, the graphics scene in Qt uses BSP to figure out what is visible, this is an extremely optimised algorithm that allows you to discard massive quantities of objects that are not visible very quickly, which is useful if you have 8 million things you're trying to render.

Your buttons do have instances, they're in the generated _moc files, the moc binary which is critical to how qt works. During the build, it takes a .ui file and turns it into an implementation and header inside your build tree, You will see at the top of your window class that it calls:

Code: Select all

 ui->setupUi(this);
This is the generated code being called to create the objects, follow that method and you'll see that everything is created.

The TableView won't show a grid because there are no items to show, it doesn't make sense for it to display anything other than a blank "table" if you have not provided a model and added data to that model, otherwise it would look like there is "blank" data in the table, which there isn't.

If you want to display data in the table, then you need to create a model instance, sometimes you can get away with using QStandardItemModel which is a generic model that can be used in the Qt containers, if that doesn't fit your need then you create a custom item model and provide all of the necessary methods that are required for a model.

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Wed May 19, 2021 1:21 pm

ok, thanks a lot, even if I still don't see how to create a model it's indeed a really cumbersome construction, not for hobbyists at all who want and need quick results, tbh...
but anyway, even an empty Excel table is not just a blank white canvas screen but shows cells/fields with grids around from the start... :roll:

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Wed May 19, 2021 8:25 pm

dsyleixa123 wrote:
Wed May 19, 2021 1:21 pm
ok, thanks a lot, even if I still don't see how to create a model it's indeed a really cumbersome construction, not for hobbyists at all who want and need quick results, tbh...
but anyway, even an empty Excel table is not just a blank white canvas screen but shows cells/fields with grids around from the start... :roll:
Spreadsheet!=Table

Excel is a spreadsheet, cells always exist in a spreadsheet. What you have created is a table, if you query a database would you expect to see 16 million blank rows in the resulting table or just the rows that match the query? Tables show data, that's the logical behaviour for a table and if you are expecting to see empty rows, how many empty rows should be displayed? 10, 100, 2^31?

The model view design is what you will find across the board, C#, Python, other C++ frameworks, Javascript (Node), PHP, swift and so on, if you want to program anything substantial then you're going to deal with this concept. Data doesn't care how it's viewed. Views don't care how the data is stored.

You can't get away from this paradigm, if you wanted to write an IOS or Android app, you're going to have to deal with this type of structure.

However, if you use the QTableWidget you can do what you want, but, you won't be able to create a huge table without it taking considerable time to populate. I would never recommend using any of the container widgets (typically they end with "Widget", so QTableWidget instead of QTableView), the reason for this is what I've been eluding to over this conversation, they're designed for simple cases and if you go down that route and then realise it was a mistake it can take a fair amount of effort to switch to using a View/Model container. Using the "Widget" containers is a false time-saver that will bite you on the ass.

If you put this in your QMainWindow constructor you will get what you want.

Code: Select all

    auto tableWidget = new QTableWidget;

    tableWidget->setRowCount(32);
    tableWidget->setColumnCount(32);

    for (auto row=0; row<tableWidget->rowCount(); row++) {
        for (auto column=0; column<tableWidget->columnCount(); column++) {
            auto cell = new QTableWidgetItem(QString("%1").arg((row*tableWidget->columnCount())+column));

            tableWidget->setItem(row, column, cell);
        }
    }

    this->setCentralWidget(tableWidget);

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Thu May 20, 2021 9:58 am

thank you very much for your explanation, that helped me a lot for understanding! :idea:

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Thu May 20, 2021 5:33 pm

dsyleixa123 wrote:
Thu May 20, 2021 9:58 am
thank you very much for your explanation, that helped me a lot for understanding! :idea:
It's not a problem. You're coming from a lack of experience with C++, a lack of experience with Qt and a lack of experience with application UI paradigms.

Trying to learn one of those at a time is hard enough, but trying to learn all of them at once is overwhelming. Just keep at it.

I employed a junior dev a few years back who had no experience with Qt and he was initially overwhelmed with the size of the desktop software I needed him to work on, he couldn't comprehend that one person (myself) had written it all. I explained the architecture of the application and how it all fits together and then left him to browse the code, I gave him the task of writing a plugin for the application and pointed him at other plugins in the code that he should look at to get an idea of how you integrate it into the application.

Qt is a huge framework, I'll let you into a secret, "professional programmers" like myself don't retain information about the API's, we remember classes and we use the documentation to find a method that we need, we know just enough and make guesses at what functions might be called or what they do, we consult the framework's documentation all the time when writing code.

Committing too much to memory is a waste, documentation is your friend (as is StackOverflow) and Qt's documentation is absolutely fantastic, it's very good and other companies (Apple) should look at Qt and strive to achieve documentation like that.

dsyleixa123
Posts: 1482
Joined: Mon Jun 11, 2018 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Sat May 22, 2021 6:30 pm

hi,
just for learning I tried out and added your code snippet, and good news:
it can be compiled
but bad news:
it crashes immediatly after showing up the form.
OTOH, if I outcomment the new snippet, it all runs fine like before.

compared to the implementation of ui and scene the lines look like this:

Code: Select all

// MainWindow::MainWindow() 
   ui->setupUi(this);   
   scene  = new QGraphicsScene(this);
   auto tableWidget = new QTableWidget;
https://github.com/dsyleixa/RaspberryPi ... _NAND_tabl

this raises 3 questions:
1) why does it start but then crashes?
2) why is there an "auto" before QTableWidget ?
3) why is there not (this) behind QTableWidget ?
:|
Last edited by dsyleixa123 on Sun May 23, 2021 9:27 am, edited 1 time in total.

Heater
Posts: 18254
Joined: Tue Jul 17, 2012 3:02 pm

Re: qt5 creator: added a QTableView but no grids to see

Sat May 22, 2021 7:55 pm

dsyleixa123 wrote:
Sat May 22, 2021 6:30 pm
this raises 3 questions:
1) why does it start but then crashes?
This is a common occurrence in C and C++. Could be:
a) Making an array access using an index beyond its bounds.
b) Dereferencing a null or uninitialised pointer.
c) Returning a pointer to some memory/object that was declared on the stack.
d) Likely a few other similar memory use mistakes.

Sorry I don't know about this particular case.
dsyleixa123 wrote:
Sat May 22, 2021 6:30 pm
2) why is there an "auto" before QTableWidget ?
"auto" is used in place of some actual type in declarations to indicate that the named variable is of whatever type the expression on the right hand side returns. It saves having to write out that type or even knowing what it is.

When writing:

Code: Select all

auto tableWidget = new QTableWidget;
it means that "tableWidget" will be of whatever type "new QTableWidget" creates. Which of course is pointer to "QTableWidget". Saves a bit of repetition in typing in this case.
dsyleixa123 wrote:
Sat May 22, 2021 6:30 pm
3) why is there not (this) behind QTableWidget ?
:|
I suspect that this might be a clue to your crash. As it stands tableWidget is an object that is declared on the stack. It will be destroyed when whatever function that declaration is returns. I suspect that tableWidget should be declared as a member of whatever class that method is in. Thus avoiding it being deleted when the method returns.

Not totally sure mind.
Last edited by Heater on Sat May 22, 2021 9:34 pm, edited 2 times in total.
Memory in C++ is a leaky abstraction .

swampdog
Posts: 698
Joined: Fri Dec 04, 2015 11:22 am

Re: qt5 creator: added a QTableView but no grids to see

Sat May 22, 2021 8:23 pm

fizzyade wrote:
Thu May 20, 2021 5:33 pm
dsyleixa123 wrote:
Thu May 20, 2021 9:58 am
thank you very much for your explanation, that helped me a lot for understanding! :idea:
Qt is a huge framework, I'll let you into a secret, "professional programmers" like myself don't retain information about the API's, we remember classes and we use the documentation to find a method that we need, we know just enough and make guesses at what functions might be called or what they do, we consult the framework's documentation all the time when writing code.

Excellent piece of advice there. I don't remember how to do anything. I remember how to find it out.

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Sat May 22, 2021 8:31 pm

dsyleixa123 wrote:
Sat May 22, 2021 6:30 pm
this raises 3 questions:
1) why does it start but then crashes?
The code snippet I posted is working code, I'd need to see the entire function or more ideally, the MainWindow.cpp file.
Heater wrote:
Sat May 22, 2021 7:55 pm
This is a common occurrence in C and C++. Could be:
a) Making an array access using an index beyond its bounds.
b) Dereferencing a null or uninitialised pointer.
c) Returning a pointer to some memory/object that was declared on the stack.
d) Likely a few other similar memory use mistakes.

Sorry I don't know about this particular case.
All of these are reasons why I tell beginners to avoid C++, pointers are something that can still trip experienced programmers up, especially when it comes to indirections, endianness and so on. No garbage collection either, so managing the lifecycle of objects can get complex, smart pointers can help, but they're not the be-all and end-all.
dsyleixa123 wrote:
Sat May 22, 2021 6:30 pm
2) why is there an "auto" before QTableWidget ?
auto produces cleaner looking code IMHO, especially when defining sets of local variables where everything lines up in the same place. Its best use is in trailing return types, again it produces cleaner looking code and the declarations aren't backwards.
dsyleixa123 wrote:
Sat May 22, 2021 6:30 pm
3) why is there not (this) behind QTableWidget ?
:|
I don't understand? Do you mean

Code: Select all

auto tableWidget = new QTableWidget(this)
instead of:

Code: Select all

auto tableWidget = new QTableWidget;
The former makes the table widget a child of the parent at the point of allocation, it's by definition a sub-window of the parent window (when I say window, there's generally no differentiation between a say QButton and a QWindow, you can create a Button as a top-level window and you'll end up with a Button with a title bar.

My example code sets the centralWidget of the QMainWindow (centralWidget is something specific to QMainWindow, other top-level Qt widgets don't use that mechanism, you create layouts and then add widgets to the layout, you then set the layout as the layout of the window - you can do the same with QMainWindow, I just chose centralWidget as it was an easier thing to show.
Heater wrote:
Sat May 22, 2021 7:55 pm
I suspect that this might be a clue to your crash. As it stands tableWidget is an object that is declared on the stack. It will be destroyed when whatever function that declaration is returns. I suspect that tableWidget should be declared as a member of whatever class that method is in. Thus avoiding it being deleted when the method returns.

Not totally sure mind.
No, it's using new, it's allocated on the heap and not the stack. However, it was just a basic example, they will, of course, have to store it in a member variable otherwise accessing it later on, will be a bit tricky for a newbie (although not impossible).

fizzyade
Posts: 52
Joined: Wed Aug 14, 2019 10:34 am

Re: qt5 creator: added a QTableView but no grids to see

Sat May 22, 2021 8:38 pm

swampdog wrote:
Sat May 22, 2021 8:23 pm
Excellent piece of advice there. I don't remember how to do anything. I remember how to find it out.
I suffer from hepatic encephalopathy so it's not even so much a choice I get to make, but once you are fluent in one language the skills transfer across to other languages and the same for the processes you go through to write code. I at one time wrote commercial CAD software, it was pure Windows API (not MFC) and as you might expect was a huge project and in addition to lots of C, there was lots of assembler as well, we didn't use Windows GDI to draw, we wrote our own graphics library and then blit blatted it to the window as necessary, same for the spacial database.

Back then it was Visual Studio 4 IIRC, finding stuff in a project in VS4 was horrific, there was a third party plugin though that was kick-ass, it had fuzzy matching and was super quick, without that tool it would have been horrible to use, but that method hasn't changed, remember enough so you can look up the answer. We also didn't have any SCM, we worked in isolation on different parts of the software, once we'd finished our bit we merged - by hand. That is not a fun experience, and it's especially not fun on a very large project.

Heater
Posts: 18254
Joined: Tue Jul 17, 2012 3:02 pm

Re: qt5 creator: added a QTableView but no grids to see

Sat May 22, 2021 9:40 pm

fizzyade wrote:
Sat May 22, 2021 8:31 pm
No, it's using new, it's allocated on the heap and not the stack. However, it was just a basic example, they will, of course, have to store it in a member variable otherwise accessing it later on, will be a bit tricky for a newbie (although not impossible).
Quite so. I just realise day mistake and came back to fix it. You beat me to it.

I was expecting to see something like:

Code: Select all

this.tableWidget = new QTableWidget(this)
to make it easy to use later as you say.
Memory in C++ is a leaky abstraction .

Return to “C/C++”