Connecting a function to a tab in Wt - c++

I am using the Wt library to write in C++ a website. I would like to use tabs on that website. To do so I use the WTabWidget.
In the documentation they make a tab and link each tab to a function :
Wt::WTabWidget *examples = new Wt::WTabWidget(this);
examples->addTab(helloWorldExample(), "Hello World");
examples->addTab(chartExample(), "Charts");
examples->addTab(new Wt::WText("A WText"), "WText");
Based on that I wrote this :
WTabWidget *myTab = new WTabWidget();
myTab->addTab(test(), "Test Tab");
But my compiler tells me :
error: cannot initialize a parameter of type 'Wt::WWidget *' with an rvalue of type 'void'
My "test" function has return type void. It's logic an rvalue of type void cannot be assigned to a parameter of type "WWidget*".
But if they show that example in the documentation, why can't I do it?
that : examples->addTab(chartExample(), "Charts");
Thanks for your help!

But if they show that example in the documentation, why can't I do it?
Their example function returns a widget, so you should do the same:
Wt::WWidget* test()
{
Wt::WText *text = new Wt::WText("This is a test tab text");
return text;
}

Related

How to pass a function pointer to a method so that it can be used in Qt's connect()?

Background: I want to dynamically create the structure of a context menu and pass the slot of the action items to the method that creates the context menu.
The actual slot is in a QWidget class. I have tried different solutions by passing a function pointer. But they don't compile. Typical error message: "cannot initialize a parameter of type 'void (*)()' with an rvalue of type 'void (TextEdit::*)()'"
This compiles, but does not trigger the desired event:
MenuBuilder builder(&parentMenu);
auto *subMenu = builder.createMenu(SECTION, this, SLOT(TextEdit::onInsertChars()));
And the corresponding method:
QMenu *MenuBuilder::createMenu(const MenuDescription &menuDescription,
const QObject *receiver, const char *target) {
...
inlineMenu->addAction(text, receiver, target);
...
}
I'm sure there's an obvious solution, but I can't figure it out.
The solution: In this context you have to pass SLOT(onInsertChars()) instead of SLOT(TextEdit::onInsertChars()).
auto *subMenu = builder.createMenu(SECTION, this, SLOT(TextEdit::onInsertChars()));
The above should be:
auto *subMenu = builder.createMenu(SECTION, pointerToTheTextEdit, SLOT(onInsertChars()));
(of course if the onInsertChars() method is a slot in the class whose method is making the above call, then you can pass this as the pointer to the object that has the onInsertChars() slot)
Also, you may want to rename const char * slot in your createMenu() function to something else, as Qt's MOC preprocessor has kind of claimed the word slot for its own purposes and that might cause problems for you if you try to use it as a parameter name. Maybe rename the parameter to const char * slotName or something instead.

How to modify a Button Content from a function (c++ UWP app)?

Currently learning C++ and using Visual Studio 2017. My UWP app have 10 buttons (named b0-b9) and I want to create a function that will manage the content change of the buttons.
For this I need to pass the button name and the content. I want to modify to the function but I don't know how to do it.
It would look something like this:
void contentButtonChange(Button BtnName, String myString)
{
BntName->Content = myString;
}
Main()
{
.....
contentButtonChange(b0, string1);
contentButtonChange(b1, string2);
contentButtonChange(b2, string3);
.....
}
added note: I'm currently able to change the Content of the button from the Main but I'm unable to write a function that will accept a Button as a parameter. I'm always getting an error no matter what I try.
In the example above BtnName in the function is highlighted with the error: expression must have a pointer or handle type
I found how to do it. I need to add this to my function call:
Windows::UI::Xaml::Controls::Button^ btnName
like this:
void contentButtonChange(Windows::UI::Xaml::Controls::Button^ btnName, Platform::String^ myString)
{
bntName->Content = myString;
}
works now.
You need to use TextBlock to set to the Button.
void contentButtonChange(Button BtnName, string myString)
{
BntName->Content = new TextBlock() { Text = myString };
}

How does passing Objective-C object as argument to C++ method work?

I was reading source code of WebKit yesterday when I found some lines of code that I can't understand how it works.
There is a C++ method declared in WebPageProxy.h as this:
RefPtr<API::Navigation> loadRequest(const WebCore::ResourceRequest&, WebCore::ShouldOpenExternalURLsPolicy = WebCore::ShouldOpenExternalURLsPolicy::ShouldAllowExternalSchemes, API::Object* userData = nullptr);
It is called in WKWebView.mm like this:
- (WKNavigation *)loadRequest:(NSURLRequest *)request
{
auto navigation = _page->loadRequest(request);
if (!navigation)
return nil;
return [wrapper(*navigation.leakRef()) autorelease];
}
The argument request is of type NSURLRequest *, but the type in the declaration of the method is const WebCore::ResourceRequest&. I can't fully understand it.
Also, after setting some breakpoints and experimenting, I found that this constructor of the ResourceRequest class is being called:
ResourceRequest(NSURLRequest *nsRequest)
: ResourceRequestBase()
, m_nsRequest(nsRequest)
{
}
I'm not familiar with C++. Can someone help me understand how does this work?

How to get a member function pointer in MSVC?

I'm not going to get into too much of the details on the Excel side of things, I essentially took code from this example:
C++ app automates Excel (CppAutomateExcel)
solution1.cpp
So I've tried this code in MSVC and it compiles:
class foo { public: virtual void bar(){} };
int main()
{
void (foo::*p)() = &foo::bar;
}
But similar code to capture the address of the move function in Excel does not work:
int main()
{
Excel::_ApplicationPtr spXlApp;
HRESULT hr = spXlApp.CreateInstance(__uuidof(Excel::Application));
Excel::WorkbooksPtr spXlBooks = spXlApp->Workbooks;
Excel::_WorkbookPtr spXlBook = spXlBooks->Add();
Excel::_WorksheetPtr spXlSheet = spXlBook->ActiveSheet;
HRESULT(Excel::_Worksheet::*pMove)(...) = &spXlSheet->Excel::_Worksheet::Move;
<... irrelevant code ...>
return 0;
}
This has the following compiler error:
error C2276: '&': illegal operation on bound member function expression
If I remove the &, it says I should add it back:
error C3867: 'Excel::_Worksheet::Move': non-standard syntax; use '&' to create a pointer to member
Any help on what to do here would be greatly appreciated.
You say in your question "but similar code..." and then you show code in which you do not do the same thing. Try using the same syntax for setting pMove as you used for setting p in your smaller example. Try something like &Excel::_Worksheet::Move; (without the "spXlSheet->").
If you can specify the specific instance of the object for which to call the function pointer at the time that you set the function pointer as you have there, I'm not aware of such a capability. After dropping spXlSheet-> from where you set the variable, use it instead where you want to call the function pointer.
You need to declare the method pointer like this instead:
// or whatever parameter type Move() actually uses...
void (Excel::_Worksheet::*pMove)(tagVARIANT, tagVARIANT) = &Excel::_Worksheet::Move;
Then, to actually call pMove(), you would have to do something like this:
Excel::_WorksheetPtr spXlSheet = ...;
(spXlSheet.Get()->*pMove)(...);

Using QScriptEngine with a "native function" call... QT Example... fails: "no matching function for call to 'QScriptEngine::newFunction' "?

I am using an example provide by Qtcreator and getting this error... ??
void MainWindow::hBtn
{
QScriptEngine e;
QScriptValue fun = e.newFunction(myAdd); // ERROR: No matching function...
e.globalObject().setProperty("myAdd", fun);
QScriptValue result = e.evaluate("myAdd(myNumber, 1)");
}
QScriptValue myAdd(QScriptContext *context, QScriptEngine *engine)
//also tried: QScriptValue MainWindow::myAdd(QScriptContext *context, QScriptEngine *engine) //fails as well with same ERROR
{
QScriptValue a = context->argument(0);
QScriptValue b = context->argument(1);
return a.toNumber() + b.toNumber();
}
Example: http://harmattan-dev.nokia.com/docs/library/html/qt4/qscriptengine.html
scroll down to "Native Functions"
Looked through another user having problems with no solution either: Using a member function with QScriptEngine::newFunction
Try it with an int for the second parameter. QScriptValue is not int related.
Edit: Ok, just realized that there is another possible combination using QScriptValue, but you may have to pass also an int and make fun a constant.
Hope it helps.
Used as basis: Pass member function pointer in C++
I took the "myAdd" declaration out of the header class... problem solved.