|
Yes, I am now using plain function to analyze results of "connect".
My code is still pretty messy , hard to read , since I added debug...
But I can step thru it , so the debug is "leftover from lambda"...
I got my code doing what I need...
In retrospect - the problem was what I suspected from the start
understanding "connect" and PLACING it into correct place.
PS
As far as getting help from forums - it was ABOUT 80% of "RTFM" and other
"fill the blanks" to make "small talk"...
|
|
|
|
|
Salvatore Terress wrote: then I
will take it down.
Is that OK? No, because that leaves orphaned messages which make little sense to others reading it.
Incidentally, I did look at this code earlier and tried to make it readable, by removing all those extra braces in the first sample, but it still made little sense. As to the second sample, why are you trying to use a lambda expression for all that code? Use a proper function/member subroutine so you can see better what is (supposed to be) happening. And, as k5054 says, this is most likely a Qt issue rather than simple C++.
|
|
|
|
|
Richard MacCutchan wrote: why are you trying to use a lambda expression for all that code?
I agree with that strongly.
If the developer can't figure out the lambda code then they shouldn't be using it in the first place. That isn't being denigrating since to me they are almost always confusing so I never voluntarily use them. They do nothing but make the code obtuse and the more complicated it is the more that becomes true.
It also has the following impacts.
- Maintenance programmers must figure it out.
- I have seen more than one senior developer use it incorrectly making the code less efficient.
Seems almost like there could be a coding guideline in that. If methods must be, for example, less than 100 lines of code, then seems that lambda expressions should also be limited perhaps to something like 10 clauses (or less.)
|
|
|
|
|
Please read my previous reply.
It was not my choice to use lambda - I took the code from another forum contributor...
Using lambda was not that difficult - the code flow was pretty logical.
The issue was - for whatever reason I could not "step thru" the lambda code...
|
|
|
|
|
I fully understand that. What I'm saying is that there is no reason that you to have to use a lambda. You can extract the function body and use a normal function pointer instead. And a normal function can be debugged by putting a break point a the function entry point.
I'm not sure what your stumbling point is here. Consider:
struct S {
int i;
double d;
};
bool cmp_double(const S& s1, const S&s2) {
return s1.d < s2.d;
}
int main()
{
std::vector<S> vs;
std::sort( vs.begin(), vs.end(), cmp_double);
std::sort( vs.begin(), vs.end(), [](const S& x, const S&y) { return x.d < y.d } );
}
The point is, the two sorts do exactly the same thing. So there's no reason you can't extract the lambda and use the named function as a parameter. If you're having trouble understanding this, you need to go back to your study material and review lambdas. I can recommend Justin Turners C++ weekly you-tube blog [https://www.youtube.com/playlist?list=PLs3KjaCtOwSZ2tbuV1hx8Xz-rFZTan2J1](https://www.youtube.com/playlist?list=PLs3KjaCtOwSZ2tbuV1hx8Xz-rFZTan2J1) Just follow the link and then search for lambda, and watch the videos. It might be worth doing even if you do understand lambdas. It's not like another perspective can't help with the understanding.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
Salvatore Terress wrote: It was not my choice to use lambda - I took the code from another forum contributor
Pretty sure that is a choice. As suggested in the other post you can rewrite it.
Not sure I have ever used a code sample where I did not modify it.
|
|
|
|
|
|
It counts the initial characters in the string until the pointer is aligned on a longword boundary. It then uses some clever bit manipulations to count the rest of the characters four at a time. If you run that code in the debugger you will be able to see exactly what happens.
|
|
|
|
|
Presumably already clear what the 4/8 block is used in the first place.
Googling with following provides some answers.
explain strlen himagic lomagic
Following line is the key
if (((longword - lomagic) & ~longword & himagic) != 0)
I tried eye balling that and using examples in my head but still not clear. I believe it is relying on arithmetic overflow.
If I wanted to understand that I would write up some test code with examples characters (4 blocks) with zero at the end (position 4, 3, 2, 1). Then have it print the results of each clause in the above if using binary representation (1 and 0) to see how the bits look for each different example and for each part of the clause.
Perhaps as a learning experience also copy that code into your own space and then write a test jig to time results via that and using the more straightforward (just by char) code. You should start with a large number of runs like probably at least 100,000.
|
|
|
|
|
More update
Found the crasher - wrong "exit" from "for" loop looking for main menu.
Having issues (expected ) using other than first main menu...
UPDATE
I am posting this to show SOME progress.
I did pepper the original with handful of "debug"...
( I need to add comments about what the code does...)
So far it works with main menu = 0 and all its sub menus - SUCCESS.
I got the sub menu index - so part of my goal is done.
Now I need to find the "main menu" index and then find the "crasher"..
BTW the author deserves LARGE credit for the code..
int MainWindow_Bluetooth::setupLambda ( void )
{
#ifdef LAMBDA
text = "START TASK DEBUG lambda ... ";
text += Q_FUNC_INFO;
text += QString::number(__LINE__);
qDebug() << text;
#endif
QObject obj;
qDebug() << " index_sub " << index_sub ; obj.connect(subMenu[index_sub],&QMenu::triggered,subMenu[index_sub ],[](QAction* action)
{
QString path=action->text();
#ifdef LAMBDA
qDebug() << "TEST need index ?? " << path;
qDebug()<<"path (sub menu trigger ) "<< path ;
#endif
QWidget* parent=action->parentWidget();
path = QString("(%1)").arg(parent->actions().indexOf(action));
#ifdef LAMBDA
qDebug()<<"path (sub menu ) with index TOK "<< path ;
#endif
while(parent)
{
QMenu* menu=qobject_cast<QMenu*>(parent);
QString title=menu->title();
#ifdef LAMBDA
qDebug()<<"title (main menu ) "<< title ;
#endif
path+="->"+title;
qDebug()<<"path (title) "<< path ;
parent=parent->parentWidget();
#ifdef LAMBDA
qDebug()<<"TRACE "<< __LINE__ ;
#endif
if(parent)
{
QMenu* menu=qobject_cast<QMenu*>(parent);
#ifdef LAMBDA
qDebug()<<"TRACE "<< __LINE__ ;
#endif
int index=0;
const QList<QAction*> actions=menu->actions();
for(const QAction *act : actions)
{
#ifdef LAMBDA
qDebug()<<"TRACE "<< __LINE__ ;
#endif
if(act->text()==title)
{
path+=QString("(%1)").arg(index);
#ifdef LAMBDA
qDebug()<<"path (main menu -> sub menu) "<< path ;
#endif
break;
}
#ifdef LAMBDA
qDebug()<<"TRACE "<< __LINE__ ;
#endif
index++;
}
}
}
qDebug()<<" FINAL (??) "<< path;
} );
#ifdef LAMBDA
text = "END TASK DEBUG lambda... ";
text += Q_FUNC_INFO;
text += QString::number(__LINE__);
qDebug() << text;
#endif
return 0;
}
OUTPUT
11:57:03: Starting /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/mdi/mdi...
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
QObject::connect(QAction, Unknown): invalid nullptr parameter
"START TASK DEBUG lambda ... int MainWindow_Bluetooth::setupLambda()89"
index_sub 0
"END TASK DEBUG lambda... int MainWindow_Bluetooth::setupLambda()167"
TEST need index ?? "SubWindow LOCAL terminal all options #0"
path (sub menu trigger ) "SubWindow LOCAL terminal all options #0"
path (sub menu ) with index TOK "(0)"
title (main menu ) "terminal # 0"
path (title) "(0)->terminal # 0"
TRACE 128
TRACE 134
TRACE 141
TRACE 153
TRACE 141
TRACE 153
TRACE 141
TRACE 153
TRACE 141
TRACE 153
TRACE 141
TRACE 153
TRACE 141
path (main menu -> sub menu) "(0)->terminal # 0(5)"
title (main menu ) "Window control"
path (title) "(0)->terminal # 0(5)->Window control"
TRACE 128
TRACE 134
11:57:22: /mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/mdi/mdi crashed.
I do not get what is missing - the error "points" at "action".
Code snippet :
QObject::connect(&MainWindow_Bluetooth::subMenu,&QMenu::triggered,&MainWindow_Bluetooth::subMenu,[](QAction* action)
{ QString path=action->text();
QWidget* parent=action->parentWidget();
path+=QString("(%1)").arg(parent->actions().indexOf(action));
while(parent)
{
QMenu* menu=qobject_cast<QMenu*>(parent);
QString title=menu->title();
path+="->"+title;
parent=parent->parentWidget();
if(parent)
ERROR :
/mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/terminal_Bluetooth/mainwindow_Bluetooth_copy.cpp:42: error: C++ requires a type specifier for all declarations
mainwindow_Bluetooth_copy.cpp:42:10: error: C++ requires a type specifier for all declarations
QObject::connect(&MainWindow_Bluetooth::subMenu,&QMenu::triggered,&MainWindow_Bluetooth::subMenu,[](QAction* action)
^
modified 3-Feb-24 16:02pm.
|
|
|
|
|
No, the error is on QObject::connect(... . You cannot call a member function of a class(*). You have to insantiate an object of the class and call the member function on that object. Something like:
QObject obj();
obj.connect();
[*] Unless the member function is a static member function. I doubt that is the case here.
Mircea
|
|
|
|
|
Certainly looks to me that at a minimum you are missing a paren in the first line/statement.
|
|
|
|
|
That's what I thought too, but then I saw the comment //lambda just after the opening brace. I think that the following code is all a lambda expression as a parameter to the QObject::connect() call. I think the OP could have cleaned up the submission with something like
QObject::connect(&MainWindow_Bluetooth::subMenu,&QMenu::triggered,&MainWindow_Bluetooth::subMenu,[](QAction* action){ });
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
I suggest looking through all your code (there really is no "points" when something comes to an "action" especially when ALL of the code is not available) for "typedef". Then, assuming these errors are sallying forth from the mingw/gnu/etc compiler you're using, and have no code associated with them to differentiate one from the other, change any typedefs of the form:
(this only an example, not the code you have)
std::vector<int>::size_type
To
std::vector<int>::size_type size
See if any errors go away.
modified 2-Feb-24 14:34pm.
|
|
|
|
|
Thanks, the code is an example I found, so I am just starting to modify it for my use.
I stopped when I received the error.
I actually got more , but I just do not see how to fix the "action".
The "problem" is - the original error "pointed " on "connect" and this error "points " on "action"
Let me work on this...maybe I can actually clean up the whole "lambda".
|
|
|
|
|
Here is modified , incomplete , code .
int MainWindow_Bluetooth::setupLambda ( void )
{
#ifdef LAMBDA
text = "START TASK DEBUG LAMBDA... ";
text += Q_FUNC_INFO;
text += QString::number(__LINE__);
qDebug() << text;
#endif
QObject obj();
obj.connect(&subMenu[index],&QMenu::triggered,&subMenu[index],[](QAction* action)
{
}
);
#ifdef LAMBDA
text = "END TASK DEBUG LAMBDA... ";
text += Q_FUNC_INFO;
text += QString::number(__LINE__);
qDebug() << text;
#endif
return 0;
}
Now I am getting this error
/mnt/A_BT_DEC10/BT__PROGRAMS/A_JAN11/A_BT_LIBRARY/terminal_Bluetooth/mainwindow_Bluetooth_copy.cpp:92: error: base of member reference is a function; perhaps you meant to call it with no arguments?
I am still lost...
(I do not like lambda - too cryptic )
|
|
|
|
|
You are getting somewhere. Try this:
QObject obj;
Salvatore Terress wrote: (I do not like lambda - too cryptic ) Tough!
Mircea
|
|
|
|
|
Salvatore Terress wrote: (I do not like lambda - too cryptic )
There's nothing that says you have to use a lambda. Sometimes, the capture variables are very useful, but if you'd rather write a normal function, the go ahead and do so.
Sometimes a function object works best: Function Objects in the C++ Standard Library | Microsoft Learn
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
I am using this code example to get more familiar with "connect".
I am at 3rd attempt to write easy to understand way to process menu / submenu code.
I had it working at one time, for one menu. Now I am adding more menu and now my submenus are "multiple selection" instead of single submenu.
This code example seems to have that "fixed" , but now it is using lambda...
|
|
|
|
|
hi,
VS=2015
in my mfc dialog class that is inherited from CDialogEx, i can find PreTranslateMessage method?.
I select the myDlg class, and there is no such method. Need it for some char exclusions inside edit box.
There is no Onchar method for edit box either.
|
|
|
|
|
Open ClassWizard and and add virtual method PreTranslateMessage.
Then select the message WM_CHAR to handle it in OnChar
|
|
|
|
|
How to? I know the basic printing functions and using the CPrintDialog plus PRINTDLG pd,
a little sample that only prints a statement but shows what I am using:
CPrintDialog dlg(FALSE, pd.Flags);
if (dlg.DoModal() != IDOK)
{
AfxMessageBox("Abort or Unknown Printer or Printer device error");
return;
}
pd.hDC = dlg.CreatePrinterDC();
ASSERT(pd.hDC !=0);
CDC * dc = new CDC;
dc = CDC::FromHandle(pd.hDC);
int cxPage = ::GetDeviceCaps (pd.hDC, HORZRES) ;
int cyPage = ::GetDeviceCaps (pd.hDC, VERTRES) ;
sizePrn.cx = cxPage;
sizePrn.cy = cyPage;
memset(&lf, 0, sizeof(lf));
lf.lfHeight = -MulDiv(12, dc->GetDeviceCaps(LOGPIXELSY), 72);
lf.lfWeight = FW_BOLD;
lf.lfPitchAndFamily = FIXED_PITCH | FF_MODERN;
lf.lfQuality = PROOF_QUALITY;
lstrcpy(lf.lfFaceName, "Times New Roman");
VERIFY(font.CreateFontIndirect(&lf));
dc->SetMapMode (MM_ISOTROPIC) ;
dc->SetWindowExt ( 1000, 1000) ;
dc->SetViewportExt (cxPage / 2, -cyPage / 2) ;
dc->SetViewportOrg (cxPage / 2, cyPage / 2) ;
dc->SetTextAlign(TA_BASELINE | TA_CENTER);
dc->SetBkMode(OPAQUE);
dc->SetMapMode(MM_TEXT);
dc->PatBlt(0, 0, sizePrn.cx, sizePrn.cy, WHITENESS);
dc->LPtoDP(&sizePrn);
dc->SelectObject(&font) ;
CSize extentChar = dc ->GetTextExtent("M",1);
int nCharHeight = extentChar.cy+4;
int nCharWidth = extentChar.cx+10;
BeginWaitCursor();
CString printDate = GetMyCurDateTime();
DOCINFO docinfo;
memset(&docinfo, 0, sizeof(docinfo));
docinfo.cbSize = sizeof(docinfo);
docinfo.lpszDocName = _T("Calif Lottery Winnings");
docinfo.fwType = 0;
rc = dc->StartDocA(&docinfo);
if (rc < 0)
{
sprintf(temp, "Unable to Begin printing - Error[%d]", rc);
MessageBox(temp, NULL, MB_OK);
dc->ReleaseAttribDC();
dc->ReleaseOutputDC();
dc->DeleteTempMap();
EndWaitCursor();
DeleteDC(pd.hDC);
if(pd.hDevMode != NULL)
GlobalFree(pd.hDevMode);
if(pd.hDevNames != NULL)
GlobalFree(pd.hDevNames);
dc = 0;
return;
}
szTitle=CString("California Lottery System Printout ") + printDate;
dc -> StartPage();
dc -> SetTextAlign(TA_LEFT | TA_TOP);
dc -> TextOut(0, 0, szTitle, szTitle.GetLength() );
dc -> MoveTo( 0, nCharHeight );
dc -> LineTo(dc -> GetTextExtent(szTitle, szTitle.GetLength()).cx, nCharHeight);
nStart =1;
And goes on to print 1 page.
What I want to do is to place a non-character on a ticket but not like an 'X' but a block like a pencil fill-in mark at a specific place on the paper. I need to supply the coordinates for the mark and do that for the full page. Has anybody done something like this before? if so, give me some hints.
Thanks
Craig
|
|
|
|
|
When things get too complicate (re: printing), I create "forms" / pages / windows that match the required output; then "capture" the visual and print that in one go (as an "image" or whatever). Total WYSIWYG. Users can even "update" the "report" (if you let them).
"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
|
|
|
|
|
That is a good idea, will look into it.
|
|
|
|
|
I am debugging / modifying my working loop code and like to insert test code and "skip " the rest of large loop.
What would be a cool way to accomplish that...?
I have been "commenting out " temporary unwanted code , but that is pretty clumsy when large code blocks are commented out.
for (index = 0; index < list.size(); ++index)
{
... working code
{
...insert test block
}
... skip this working code
} loop end
|
|
|
|
|