cant connect doublespinbox valuechanged signal [duplicate] - qt

This question already has answers here:
Connecting overloaded signals and slots in Qt 5
(4 answers)
Closed 2 years ago.
I need to connect a signal and slot
connect(ui->doubleSpinBox_vertical, &QDoubleSpinBox::valueChanged, this, &MainWindow::updateThreshold);
void MainWindow::updateThreshold()
{
const double threshold = ui->spinBox->value();
int labelY = qRound(threshold / ui->progressBar->maximum() * ui->progressBar->height());
ui->label_2->move(0, ui->progressBar->height() - labelY); // y is inverted
}
Here I get following error:
mainwindow.cpp:14:5: error: no matching member function for call to 'connect'
qobject.h:208:36: note: candidate function not viable: no overload of 'valueChanged' matching 'const char *' for 2nd argument
qobject.h:211:36: note: candidate function not viable: no overload of 'valueChanged' matching 'const QMetaMethod' for 2nd argument
qobject.h:463:41: note: candidate function not viable: no overload of 'valueChanged' matching 'const char *' for 2nd argument
qobject.h:228:43: note: candidate template ignored: couldn't infer template argument 'Func1'
qobject.h:269:13: note: candidate template ignored: couldn't infer template argument 'Func1'
qobject.h:308:13: note: candidate template ignored: couldn't infer template argument 'Func1'
qobject.h:260:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided
qobject.h:300:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided
What mistake I'm doing here?

QDoubleSpinBox::valueChanged is overloaded so you need to resolve it manually...
connect(ui->doubleSpinBox_vertical,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this,
&MainWindow::updateThreshold);

Related

manage_[post_type]_posts_custom_column not passing `$post_id`

In Wordpress, I get the following error:
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function processar_colunas_tc_events(), 1 passed in /var/www/html/wp-includes/class-wp-hook.php on line 289 and exactly 2 expected
... when I do this:
add_action('manage_tc_events_posts_custom_column', 'processar_colunas_tc_events');
function processar_colunas_tc_events($column, $post_id) { ... }
Am I doing anything wrong?
See the Wordpress documentation for add_action. There are four arguments into the function. Note that the fourth argument is accepted_args and it specifies how many arguments your action accepts. Since the default of accepted_args is 1 but your action accepts 2 arguments, you need to set accepted_args to 2.

How to retrieve QPair from QVariant?

I'm doing auto data = combobox->currentData().value<QPair>(); but the compiler complains with:
[ 48%] Building CXX object src/CMakeFiles/mudlet.dir/dlgProfilePreferences.cpp.o
/home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.cpp: In lambda function:
/home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.cpp:420:81: error: no matching function for call to ‘QVariant::value()’
auto data = script_preview_combobox->currentData().value<QPair>();
^
In file included from /home/vadi/Programs/Qt/5.9/gcc_64/include/QtCore/QVariant:1:0,
from /home/vadi/Programs/Mudlet/mudlet/cmake-build-debug/src/ui_profile_preferences.h:12,
from /home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.h:27,
from /home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.cpp:25:
/home/vadi/Programs/Qt/5.9/gcc_64/include/QtCore/qvariant.h:351:14: note: candidate: template<class T> T QVariant::value() const
inline T value() const
^
/home/vadi/Programs/Qt/5.9/gcc_64/include/QtCore/qvariant.h:351:14: note: template argument deduction/substitution failed:
src/CMakeFiles/mudlet.dir/build.make:806: recipe for target 'src/CMakeFiles/mudlet.dir/dlgProfilePreferences.cpp.o' failed
As far as I see, my call is lining up with template<class T> T QVariant::value() - what's wrong?
QPair is a template class and your code for getting the value from the variant does not fully describe the type.
First you need to know what two types your QPair describes. Then you must use the following code to extract it (changing the QString and int to your pairs data types):
auto pair = combobox->currentData().value<QPair<QString, int> >();

PHP7. Reflection doesn't work from old versions php

I have a php script which was written on php 5.6.19, works on 5.3 version to, with some installed addons.
I decide to try execute it on php7.
The special of the script that I am initializing a class with parameter by reference via creating a new instance with Reflection::class. And there warning then waited variable by reference but value received.
Definition of the class' constructor method tried to create an instance from:
public function __construct($user, IDatabase &$repository, $errors = null);
Sample of code where this constructor is used:
// define manager type to create (all managers has the same constructor)
$manager = $managersNamespace . ucfirst($this->_manager) . "Manager";
// trying to create the manager
// !!!And here a Warning occurs
$reflect = new \ReflectionClass($manager);
$manager = $reflect->newInstance($user, $database, $errors);
After these I am invoking a method I need, and here the fatal error with stopped the script:
$method = "show" . ucfirst($this->_page) . "Page";
$reflect->getMethod($method)->invoke($manager);
I didn't see any changes in documentation. Anyone had the same issue?
First and foremost, why are you passing an object by reference !?
Objects have pass-by-reference semantics, forcibly trying to pass objects by reference has not made good sense since PHP 4.
Just remove the & ...
Let's ignore that, and pretend there is still a problem, so that you can try to understand what is going on.
To break down the problem, first you need to understand the distinction between a variable and an expression:
mine(1 + 2);
The argument to mine has no name, it's represented by a temporary variable in the engine: it's an expression.
mine(1);
The argument to mine has no name, it's not an expression, but a literal constant, represented by a compiler variable in the engine. It's similar to a temporary variable, a kind of constant expression.
mine($a);
The argument to mine has a name, which you can use to refer to it's value. It's a normal variable.
Only variables can be passed by reference because you cannot refer to expressions or literal constants
Next you need to understand why we pass-by-reference:
function mine(int $thing) {
$thing++;
}
$a = 1;
mine($a);
var_dump($a); // int(1)
In this code, $a is passed to mine() by value, so that the changes that mine() make to $thing are only visible inside the scope of mine. $a is unchanged after the call to mine() returns because $a and $thing are distinct, having been passed-by-value, which means it's value was copied on to the call stack for the invocation of mine().
function mine(int &$thing) {
$thing++;
}
$a = 1;
mine($a);
var_dump($a); // int(2)
In the code above, $a is passed to mine() by reference, this means that $a and $thing are no longer distinct. The changes mine() make to $thing are now visible after the call to mine() returns.
The last piece in the puzzle is Reflection:
function mine(int &$thing) {
$thing++;
}
$a = 1;
$reflector = new ReflectionFunction("mine");
$reflector->invoke($a);
The code above will raise:
Warning: Parameter 1 to mine() expected to be a reference, value given in /usr/src/php-src/refs.php on line 9
This is because ReflectionFunction::invoke and similar reflection functions (ReflectionClass::newInstance) accept their parameters by value and pass them onto the invoked function by value.
But ...
There is still a difference between pass-by-reference semantics, and passing by reference, a dangerous one:
class Foo {
public function qux() {}
}
class Bar {}
function mine(Foo &$foo) {
$foo = new Bar();
}
$foo = new Foo;
mine($foo);
$foo->qux();
Will obviously yield:
PHP Fatal error: Uncaught Error: Call to undefined method Bar::qux() in /usr/src/php-src/refs.php:16
Stack trace:
#0 {main}
thrown in /usr/src/php-src/refs.php on line 16
The declaration of mine() tells lies about the type safety of it's parameter. Type safety is only guaranteed upon entry to the function, the function body is free to break type safety, but it doesn't usually affect the caller when relying on the engines pass by reference semantics for objects.
This is an extremely scary kind of API, that should be avoided.

qRegisterMetaType usage

#include<QMetaType>
typedef QList<int> IntList;
qRegisterMetaType<IntList>("IntList");
error C2909: 'qRegisterMetaType': explicit instantiation of function template requires return type
C2909 says I need to define
template int qRegisterMetaType<IntList>("IntList");
If I define like I mentioned above then I get the below error
error C2059: syntax error : 'string'
warning C4667: 'int qRegisterMetaType(void)' : no function template defined that matches forced instantiation
why do I get this error ?
"qRegisterMetaType" is a function. It must appear in a code block.
int metatype_id = qRegisterMetaType<IntList>("IntList");
You need to add Q_DECLARE_METATYPE(IntList) before you can register it.

2 arguments in push_back

I am trying to put 2 arguments inside a vector using push_back but its giving me an error since the function is allowed to take only one argument. How can I pass 2 arguments??
Vertex Class:
template <class VertexType, class EdgeType> class Vertex{
public:
std::vector<std::pair<int, EdgeType>> VertexList;
};
Outside Vertex Class inside Main():
project3::Vertex<string, string> v1("v1");
v1.VertexList.push_back(1,"e1");
Error is :
error C2661: 'std::vector<_Ty>::push_back' : no overloaded function takes 2 arguments
IntelliSense: too many arguments in function call
You need to do
v1.VertexList.push_back(std::pair<int, EdgeType>(1,"e1"));
Try push_back(make_pair(1, string("e1")));

Resources