|
The second line of code does not compile and I get a ton of "helpful" messages.
I am asking the forum to help me to understand the messages.
I am not asking for RTFM...
If somebody is willing to help I will post the errors.
QAction tempAction;
subMenu[index]->addAction(" test ");
subMenu[index]->addAction(tempAction.setCheckable(true));
|
|
|
|
|
Posting such a question without the error message is not very helpful. Please make sure you post all the relevant information.
|
|
|
|
|
Don't make a post with a teensy bit of code and no errors, then ask "is someone willing to help?" Because if you don't supply ALL of the information on the problem, the answer is going to be NO. All you're doing is wasting time because now you have to wait for an answer of "Sure!" before you post the rest of the information required to solve the problem, then wait some more for an actual answer.
|
|
|
|
|
Best guess: you're mixing types; which only works for things "typed as object".
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Oh, so we need to pass a little test before we're allowed to respond to your post? How childish.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Please help us to answer your post.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
As noted not enough information.
And the cause is based on what the exact type (resolved by the compiler) for 'subMenu[index]'
So for example from Qt docs a QWidget addAction() method takes a pointer to a QAction. And in that case a char pointer would not work. (I got that by RTFM by the way.)
|
|
|
|
|
jschell wrote: I got that by RTFM by the way Me too, but I couldn't be bothered to edit my original reply. This guy really takes the biscuit (cookie).
|
|
|
|
|
I think it's the same cookie under a different name.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Minor add:
I think the loop increment is wrong
I have commented out inner loop and addend two more list to the array
so the decimal count should be 5.
OK the code say Array size 4
and it misses the last array.
That should be a snap to fix...
<"TASK #ifdef FUNCTION_TEST_PROJECT int MainWindow_Bluetooth::CreateMenus_Array()79"
" index array 0 Array size 4"
" index array 1 Array size 4"
" index array 2 Array size 4"
" index array 3 Array size 4"/pre>
OK, I have plain C question about nested "loop"
My task is to loop thru two dimensional array...
The error in my code is stopping the embedded loop.
Here is a snippet of the run debug messages
here is the "problem " code
for (index_main = 0; index_main < list_array[index_array][index_main].size(); ++index_main)
What am I doing wrong in sizing the array?
Many thanks for help resolving this.
Debug
<pre lang="text">" index array 0Array size 4"
" main menus 0 SubWindow terminal (QMdiArea )main menu size 34"
" main menus 1 SubWindow hcitool (QMdiArea )main menu size 34"
" main menus 2 SubWindow bluetoothctl (QMdiArea )main menu size 34"
" main menus 3 SubWindow system (QMdiArea )main menu size 34"
ASSERT failure in QList<T>::operator[]: "index out of range", file /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qlist.h, line 575
12:23:02: /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/mdi/mdi crashed.
Array;
const QStringList list_array[10]
{
{ "SubWindow terminal (QMdiArea )",
"SubWindow hcitool (QMdiArea )",
"SubWindow bluetoothctl (QMdiArea )",
"SubWindow system (QMdiArea )" },
{ "SubsubWindow bluetoothctl help (QMdiArea )",
"SubsubWindow bluetoothctl list (QMdiArea )",
"SubsubWindow bluetoothctl devices (QMdiArea )",
"SubsubWindow bluetoothctl show (QMdiArea )" },
{ "SubsubWindow hcitool (QMdiArea )",
"SubsubWindow hcitool dev (QMdiArea )",
"SubsubWindow hcitool scan (QMdiArea )",
"SubsubWindow hcitool inq (QMdiArea )"}
};
Code:
for (index_array = 0; index_array < list_array[index_array].size(); ++index_array )
{
text = " index array ";
text += QString::number(index_array);
text += "Array size ";
text += QString::number(list_array[index_array].size() );
qDebug() << text;
for (index_main = 0; index_main < list_array[index_array][index_main].size(); ++index_main)
{
text = " main menus ";
text += QString::number(index_main);
text += " ";
text += list_array[index_array][index_main];
text += "main menu size ";
text += QString::number(list_array[index_array][index_main].size());
qDebug() << text;
}
}
modified 15-Jan-24 18:16pm.
|
|
|
|
|
I've never used Qt before but, this line seems odd,
for (index_array = 0; index_array < list_array[index_array].size(); ++index_array ) specifically
index_array < list_array[index_array].size() Maybe try something like
for (index_array = 0; index_array < list_array.size(); ++index_array )
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
modified 15-Jan-24 15:38pm.
|
|
|
|
|
Sorry, no go.
This is not about Qt - I no longer post questions about Qt - this is wrong forum for Qt.
This is plain C question - hence in "C..." sub forum.
In my view each loop has its stopping point.
The first - parent loop - needs to stop when it reaches the end
AND its "size" is correct (see debug message ) and I can run it bypassing the "inner " loop.
The size of inner loop is wrong - see debug messages posted.
Changing the parent loop size as suggested gets this error:
/mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/terminal_Bluetooth/mainwindow_Bluetooth.cpp:142: error: member reference base type 'const QStringList[10]' is not a structure or union
mainwindow_Bluetooth.cpp:142:52: error: member reference base type 'const QStringList[10]' is not a structure or union
for (index_array = 0; index_array < list_array.size(); ++index_array )
~~~~~~~~~~^~~~~
|
|
|
|
|
I think you've got your logic in a twist here. I think it should be
for(index_array = 0; index_array < list_array.size(); ++index_array )
{
for(index_main = 0; index_main < list_array[index_array].size(); ++index_main )
{
}
}
Additionally, note that you're calculating list_array.size() and list_array[index_array].size() multiple times. Perhaps better would be
const size_t list_max = list_array.size();
for( index_array = 0; index_array < list_max; ++index_array )
{
const size_t array_max = list_array[index_array].size();
for(index_main = 0; index_main < array_max; ++index_main)
{
}
Maybe, since you've declared list_array as const , the compiler will be able to optimize away the unneeded function calls, but speaking for myself, I find my version just a little easier to read and understand.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
Sorry, but the compiler till complain about this - without specifying it is an arraray as [10]
/mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/terminal_Bluetooth/mainwindow_Bluetooth.cpp:168: error: member reference base type 'const QStringList[10]' is not a structure or union
mainwindow_Bluetooth.cpp:168:39: error: member reference base type 'const QStringList[10]' is not a structure or union
const size_t list_max = list_array.size();
~~~~~~~~~~^~~~~
const size_t list_max = list_array.size();
for( index_array = 0; index_array < list_max; ++index_array )
{
// stuff
const size_t array_max = list_array[index_array].size();
for(index_main = 0; index_main < array_max; ++index_main)
{
//more stuff
}
}
|
|
|
|
|
SOLVED
Wrong logic in embedded loop - this is size of CURRENT / dynamic MAIN loop member.
Thanks very much for all the help.
Sal
for (index_array = 0; index_array < list_array[index_array].size()+1; ++index_array )
{
text = " index array ";
text += QString::number(index_array);
text += " Array size ";
text += QString::number(list_array[index_array].size() );
qDebug() << text;
for (index_main = 0; index_main < list_array[index_array].size(); ++index_main)
{
text = " main menus ";
text += QString::number(index_main);
text += " ";
text += list_array[index_array][index_main];
text += "main menu size ";
text += QString::number(list_array[index_array][index_main].size());
qDebug() << text;
}
}
return 0;
{
Found the cause of the weird count
The code counts # of characters in the member of the child array....
" index array 0 Array size 7"
" main menus 0 SubWindow terminal added space here (QMdiArea )main menu size 57"
" main menus 1 SubWindow hcitool (QMdiArea )main menu size 34"
" main menus 2 SubWindow bluetoothctl (QMdiArea )main menu size 34"
" main menus 3 SubWindow terminal (QMdiArea )main menu size 34"
" main menus 4 SubWindow hcitool (QMdiArea )main menu size 34"
" main menus 5 SubWindow bluetoothctl (QMdiArea )main menu size 34"
" main menus 6 SubWindow system (QMdiArea )main menu size 34"
ASSERT failure in QList<t>::operator[]: "index out of range", file /home/nov25-1/Qt/5.15.2/gcc_64/include/QtCore/qlist.h, line 575
modified 15-Jan-24 19:10pm.
|
|
|
|
|
To me anyway, your for loop test seems tailored to the actual counts of items and subitems, what if each QStringList had 15 strings instead of 4?
Couldn't you use containers analogous to stl contianers
something along the lines of;
QList<QStringList> myList;
QStringList strlist1 = { "Blah1", "Blah2", "Blah3" };
QStringList strlist2 = { "Blah4", "Blah5", "Blah6" };
QStringList strlist3 = { "Blah7", "Blah8", "Blah9" };
myList.push_back(strList1);
myList.push_back(strList2);
myList.push_back(strList3);
for (int mainIndex = 0; mainIndex < myList.size(); mainIndex++)
{
for (int subIndex = 0; subIndex < myList[mainIndex].size(); subIndex++)
{
}
}
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Yes, pretty much similar code.
..and the idea of "variable count ", I will eventually have a file where I will keep the data and will add to it as required - sort of database .
I messed up a little and had the main loop in the array...
It has been great "discussion" and it helped a lot...
Thanks
PS
I may post the final version ,if anybody cares.
Right now it is lot of "cut and paste" to put it in code
which actuality does puts menu /submenu into main code.
|
|
|
|
|
SOLVED
EDIT
voluntarily removed irrelevant code.
modified 15-Jan-24 13:11pm.
|
|
|
|
|
Sorry, but this has nothing to do with C++. Whether you like it or not this is a Qt issue, and the Qt forums are the best place for the question.
|
|
|
|
|
Quote: I am using QtCreator and if you are NOT familiar with Qt - please do not tell me to post it in Qt forum.
I have DONE that a getting nowhere fast... I am getting the usual "you do not know what you are doing"
or RTFM.
The problem with your statement is that you are assuming everyone who uses C++ is also using Qt, which is simply not the case. By not using the Qt forums, you are severely restricting the number of Qt users who can see your question, and you're waiting on the fewer people with Qt experience around here to stumble on your question. Not everyone here visits the site every single day.
|
|
|
|
|
Googling with the following gives results that look like they have examples of menu creation.
"QAction" example sub menu
Other than that given multiple questions that you have about it. You might consider the following
1. Use something else
2. The source code is available. So you can dive into that to find solutions to your problems.
I will note that I do in fact use 2 quite often. Both for Java VM (and libraries) and C# Microsoft libraries. So it is not a flippant answer. Documentation is almost never sufficient for all cases. Looking at the source code provides solutions. (Or at least in a couple of cases proved that my current solution would not work.)
|
|
|
|
|
The issue was /is the relations between QMenu and QAction...I did solved this by looking at many similar examples. But as told many times - posting it here was my mistake, lesson learn.
Thanks.
|
|
|
|
|
If a memory allocation with new fails in C++, should it return NULL or throw an exception?
I have this code:
#include <iostream>
#include <vector>
#include <unistd.h>
using namespace std;
int main() {
try{
char *p = new char[44'321'833'923];
if (!p) {
cout << "memory allocation failed";
exit(1);
}
}
catch(exception&e)
{
cout << "caught it." << e.what();
exit(1);
}
catch (...)
{
cout << "caught exception;";
exit(1);
}
return 0;
}
The class I'm taking says it returns NULL, but the code seems to throw an exception.
Anyone know?
Thanks.
|
|
|
|
|
I don’t know who teaches the class you are taking but I hope you don’t pay much for it. new has been throwing std::bad_alloc for the past 20+ years.
Also, while I don’t mind at all answering your questions, why don’t you try to googling? A search for “c++ new allocation failure” gives you many results and undoubtedly can teach you more than my limited answers.
Edit: Using exactly the search I suggested, I found this good article that discusses the issue: C++ Exceptions and Memory Allocation Failure[^].
Mircea
modified 11-Jan-24 7:50am.
|
|
|
|
|