Problem with connecting QLabel to QSlider - qt

I have a problem. I've created a class, in which I have a slider and a label. I want to connect these with the QObject::connect, but when I do it, nothing happens. Can you tell me what am I doing wrong?
My class:
class Loads :public QObject
{
Q_OBJECT
public:
QSlider slider;
QLabel label;
QMainWindow okno;
Loads();
private:
int wart;
public slots:
void zmiana(int li);
};
Class "Loads" constructor:
Loads::Loads()
{
okno.setGeometry(300,300,300,300);
label.setParent(&okno);
slider.setParent(&okno);
label.setGeometry(0,0,300,200);
slider.setGeometry(0,200,300,100);
slider.setMinimum(1);
slider.setMaximum(30);
label.setText("0");
wart=0;
QObject::connect(this, SIGNAL( slider.valueChanged(int)), this , SLOT( zmiana(int)) );
okno.show();
}
My "zmiana" slot
void Loads::zmiana(int li)
{
wart=li;
label.setText(QString::number(li));
}

QObject::connect(this, SIGNAL( slider.valueChanged(int)), this , SLOT( zmiana(int)) );
I don't think that's correct, you're connecting the signal of the Loads object to the slot but the Loads object is not the one generating the signal, the slider object is doing that.
Hence I think you'll need slider as the first argument, not this. Using this as the third argument is okay, I believe, since the slot does belong to the Loads object.

Related

signal points to slot in struct

I'd like a signal to be connected to a slot inside of a struct. My struct looks like:
//Header file
struct someStruct {
public:
int index;
public slots:
void someSlot();
};
QList<someStruct*> mListOfStructs;
and then I create a button that should forward its clicked() signal to the someSlot function.
//Source file
QPushButton *cmd = new QPushButton();
grd->addWidget(cmd, 3, 2, Qt::AlignCenter);
//grd is a QGridLayout somewhere inside the gui. I can see it and also the button.
now connection the clicked() event with the slot inside a specific struct does not work.
connect(cmd, SIGNAL(clicked()), mListOfStructs[3], SLOT(someSlot()));
some sources tell me that I have to add a metaObject or sth. I tried but it didn't work out. Maybe you know better.
I might use How to connect in Qt signal and slot in dynamically added buttons to get in slot index of added button? as workaround though.
your structure need the Q_Object meta attributes in order to emit signals and recieve slot events...
struct testStruct : public QObject
{
Q_OBJECT
public:
int index;
testStruct():index(0){}
public slots:
void someSlot()
{
qDebug() << "slot called!";
}
};
after that you can conected as usual:
testStruct *ts= new testStruct;
connect(this, SIGNAL(someSignal()), ts, SLOT(someSlot()));

QListWidget doesn't recognize signals from QTest::mouseDClick

I am trying to use QTest to test UI interactions with a QListWidget.
Interactions made from a simple click work fine (QTest::mouseClick()) but interactions from a double click do not (QTest::mouseDClick()).
Here is simplified code sample to reproduce the issue :
Dialog.h
class UILIBSHARED_EXPORT Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
int doubleClickCount = 0;
QString lastItemClicked = "";
QListWidget* GetListW();
private slots:
void on_listWidget_doubleClicked(const QModelIndex &index);
public:
Ui::Dialog *ui;
};
Dialog.cpp
QListWidget*Dialog::GetListW()
{
return ui->listWidget;
}
void Dialog::on_listWidget_doubleClicked(const QModelIndex &index)
{
lastItemClicked = ui->listWidget->item(index.row())->text();
++doubleClickCount;
}
And the test class :
class DoubleClickTest : public QObject
{
Q_OBJECT
public:
DoubleClickTest();
private Q_SLOTS:
void testDoubleClick();
};
void DoubleClickTest::testDoubleClick()
{
Dialog dialog;
dialog.show();
QListWidgetItem* item = dialog.GetListW()->item(1);
QRect rect = dialog.GetListW()->visualItemRect(item);
QTest::mouseDClick(dialog.GetListW()->viewport(), Qt::LeftButton, Qt::KeyboardModifiers(), rect.center());
QCOMPARE(dialog.doubleClickCount, 1);
}
I checked the dialog manually and the slot is called as expected.
I know this is an old topic but I have encountered the same behaviour with a QTreeView and have passed some hours to find a workaround, so I think it can be useful for someone else.
Using QTest, the signal doubleClicked is never emitted due to a part of code in sources of Qt I do not understand (qtreeview.cpp, line 1934 with Qt 5.12.1, or for others, in qabstractitemview.cpp line 1952). I don't know if it is a bug or not.
To avoid this strange code, I just added a call to QTest::mouseClick before the call to QTest::mouseDClick with the same parameters.
It worked for me because my QTreeView do nothing particular on a simple click, but it can distort tests in another case.
If anyone has a better solution I take it !

connect QPushButton and QComboBox

How can I connect a QPushButton and a QComboBox?
I created a SLOT that accepts 2 parameters, a pointer to the QComboBox and the index of the selected item:
void modificaExp::eliminaExp(QComboBox *combo,int value)
{
......
combo->removeItem(value);
....
}
the widgest are there:
QComboBox* combo=new QComboBox();
combo->addItem("ciao1");
combo->addItem("ciao44");
combo->addItem("ciao222");
combo->addItem("ciao555");
QPushButton* delButton=new QPushButton();
delButton->setText("delete");
connect(delButton, SIGNAL(clicked()), this, SLOT( eliminaExp(combo,combo->currentIndex() )));
so, when I click on delButton the element stays there. I think there is a problem in the connect command, specifically I think than the slot is not called.
Are you sure you need this slot with two parameter?
Another simple way:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
connect(deleteButton, SIGNAL(clicked(bool)), this, SLOT(deleteSlot()));
}
void MainWindow::deleteSlot()
{
comboBox->removeItem(comboBox->currentIndex());
}
The slot should have the same type and equal or less number of arguments than the signal
Declare the QComboBox and the QPushButton objects in the header
modificaexp.h
private:
QComboBox* combo;
QPushButton* delButton;
modificaexp.cpp
combo=new QComboBox();
combo->addItem("ciao1");
combo->addItem("ciao44");
combo->addItem("ciao222");
combo->addItem("ciao555");
delButton=new QPushButton();
delButton->setText("delete");
connect(delButton, SIGNAL(clicked()), this, SLOT( eliminaExp()));
Modify the slot
void modificaExp::eliminaExp()
{
combo->removeItem(combo->currentIndex());
}
Refer the Qt signal slot documentation

QDialog modality vs QMainWindow

I have a QMainWindow object parent to a QDialog Object. When I call the QDialog with exec() it stays open, but I can't use the QMainWindow at the same time. If I use show() instead, the QDialog opens and hides immediately after.
I know this relates to the modality flag of the QDialog, but it does not have a Qt::Modeless flag, so I'm a bit lost.
Question: How can I display a QDialog and still be able to interact with its parent QMainWindow?
My code for the QDialog object:
class catalog : public QDialog
{
Q_OBJECT
public:
explicit catalog(QWidget *parent = 0);
~catalog();
private:
Ui::catalog *ui;
};
How I'm calling it:
void DiagramWindow::showCatalog()
{
catalog catalog(this);
catalog.exec();
}
It closes, because QDialog::show() method is asynchronous and your catalog object is destroyed right after your code leaves DiagramWindow::showCatalog() method. You should rewrite it like this:
void DiagramWindow::showCatalog()
{
catalog *c = new catalog(this);
c->setAttribute(Qt::WA_DeleteOnClose);
c->show();
}

how to access a widget from another class

I have two classes(MyWidget,ViewContact). In MyWidget, there is a QLineEdit and QListWidget. The contents in the QListWidget changes dynamically while changes in QLineEditt.
In ViewContact class there is many widgets.The ViewContact class is called by MyWidget class.
void MyWidget::viewbind(QListWidgetItem *item)
{
LblNames *widget = (LblNames*)( listWidget->itemWidget(item) );
ViewContacts *v=new ViewContacts(widget->getLabelId());
v->show();
}
then ViewContact widget shown as a window,no problem.works fine.
while clicking an update button inside ViewContact class i need to close that window and change the text inside MyWidget .now i just close the ViewContact by this.close(); function.
I give QLineEdit as public and try to change the text. No errors occur. But no changes display in QLineEdit inside MyWidget
Add a signal in the ViewContact class and emit a signal while close ViewContact Widget.The signal should contain a string to bind your list view.Before initiating ViewContact Widget from MyWidget ,should connect the signal and setText(const QString) slot.
Add the MyWidget as a parent to your ViewContacts instance. This will allow you to call a function to update the text (and it will also fix the memory leak which you currently have in your code).
You need a constructor for your ViewContacts that takes both a parent and the labelId.
class ViewContacts : public QWidget // or whatever it is, you didn't tell
{
Q_OBJECT
public:
// I don't know the class of "LabelId" either
explicit ViewContacts(LabelId id, QObject* parent = 0) : QWidget(parent)
{
// ...
}
void updateTextInParent()
{
MyWidget* w = qobject_cast<MyWidget*>(this->parent());
if (NULL != w)
{
// w is a valid pointer to a MyWidget instance
// you can access any public fields and function through it.
}
}
};
You should also use qobject_cast or dynamic_cast in the snippet you provided because a blind cast is not safe.
This seems like a perfect time to take advantage of Qt's signals and slots. You can connect a signal that emits the value you want to update in your MyWidget object, from your ViewContacts widget.
First you'll need to create a signal that emits the changed value when the ViewContacts widget is closed.
Something like:
Q_SIGNALS:
void value_changed_signal( QString value );
Next you'll want to add a slot, it can be private, in your MyWidget parent class
The declaration would look something like:
private Q_SLOTS:
void update_text( QString value );
Finally, in your MyWidget class, somewhere after you have instantiated your ViewContacts object, connect the signal and slot with something like:
connect(new_view_contacts_object, SIGNAL( value_changed_signal( QString ) ),
this, SLOT( update_text( QString ) ) );

Resources