I want to acces the value of the QSliders that are in my QGridLayout, but I don't find the right command.
An example of what I try:
QSlider* slider=ui->gridLayout->takeAt(1)->widget();
The error message I get is:
cannot convert 'QLayoutItem*' to 'QSlider*' in initialization
Is there a command to ask the QSlider right away and not the widget?
Is it possible to cast? I tried it, but this also didn't work.
How can I solve this?
You can use QGridLayout::findChild<T> method:
QSlider *slider = ui->gridLayout->findChild<QSlider *>("widget_name"); // Name is optional
Or alternatively
QList<QSlider*> sliders = ui->gridLayout->findChildren<QSlider *>();
As pointed by G.M, QLayout::takeAt is used to safely remove and return a Widget from a QLayout, so not what you are trying to do.
Related
I have a mdiArea. I add subwindows to this mdiArea every time an image is opened. The widget I set for each subwindow is imageFileDialog which inherits from the QDialog. Within this dialog I have a spinbox. I want to be able to set the value of this spinbox for every dialog I have in the mdiArea after they have been created. I do not know how to iterate through the dialogs. I tried to think of ways to do this.
for (int j=0; j < ui->mdiArea->subWindowList().size(); j++)
{
imageFileDialog *ifd = ui->mdiArea->subWindowList()[j]->widget();
ifd->setSpinBox(0);
}
but I have an error because I cannot cast the widget as my imageFileDialog class that inherits from QDialog.
I though I might be able to set a connection upon creation of my imageFileDialog:
imageFileDialog *ifd = new imageFileDialog();
ifd->connect(this, SIGNAL(emitImageFileValue(double)), SLOT(ifd->setSpinBox(double)));
subWindow1->setWidget(ifd);
but this is unlike any connection I've tried to make before. Everything compiles fine, but the signal emitted does not reach my slot. I'm hoping someone has tried this before and has some suggestions! Thanks in advance.
Use qobject_cast for the first problem and for the second issue you need to pass only the slot name to the SLOT() macro (without ifd->), or pass the ifd pointer as separate parameter to connect, something like:
QObject::connect(this, SIGNAL(emitImageFileValue(double)), idf, SLOT(setSpinBox(double)));
Is it possible to pass parameters?
Something like this:
shortcut_key_one, channelSLot(int)
shortcut_key_two, channelSLot(int)
shortcut_key_three, channelSLot(int)
If it doesn't matter what the shortcut was, assign the shortcuts to a QAction using QAction::setShortcuts.
font_increase_action_ = new QAction(tr("&Increase font sizes"), this);
font_increase_action_->setShortcuts(QList<QKeySequence>()
<< Qt::CTRL + Qt::Key_BracketRight
<< Qt::CTRL + Qt::Key_Greater);
connect(font_increase_action_, SIGNAL(triggered()), SLOT(IncreaseFontSizes()));
If you really need to know which shortcut was pressed, you could assign each one to a separate QAction and then use the QSignalMapper.
Well, to do exactly what you're asking, you could keep track of all of your QShortcut objects and then use the sender() function inside of your slot to determine which QShortcut caused the slot to be triggered.
However, it sounds like you seriously want to consider using and overriding QWidget::keyPressEvent() instead if at all possible. It would be a lot easier to ask the QKeyEvent object which key was pressed than to do all this crazy QShortcut mess.
My goal is: user can choose file (only *mp3) and after clicking twice on it it should play (so the QString to file should be send to play() function)
Firstly I started to work with QTreeView, but it has signal when the file is selected.
So I decided to create QFileDialog and used it as widget built-in into MainWindow.
The only problem that I have, that after double-click it disappears. It is possible to avoid it?
Should I work with some QDialog::finished() signal or, QDialog::done()?
First, you can get a double-click signal from QTreeView; it's:
void doubleClicked( const QModelIndex & index );
Second, if you really want to use the QFileDialog that way, first override closeEvent( QCloseEvent * event). Inside, if you want to close the dialog, do event->accept();, otherwise just do event->ignore();. Connect to QFileDialog::currentChanged( const QString & path ); to get the filename the user double-clicks. One last thing--be sure to create the QFileDialog on the heap (using new), not on the stack (a local), and call show() on it instead of exec().
Remember that you can supply it with a parent (this) and you won't need to delete it later.
connect(file_dialog, SIGNAL(finished(int)), file_dialog, SLOT(open()));
This seems to work fine. The geometry stays fixed and it remembers the last path allright..
Hi I have a problem, in my program I have several QLabel and QTextbrowser, at times, I want each of them to display something, but I want to do this through another function called NewMessage. So NewMessage will receive message from QLabel or QTextbrowser, and process them, then display it. But the problem is I don't want to have overloading function for QLabel and QTextBrowser, I want only 1 NewMessage function that can handle message pass in by either QLabel or QTextBrowser(the objects have to pass themselves in as well), how should I do that? Does it has to do with something called casting? Thank you !
I'm not sure I fully understand what you want to achieve. May be you could start by reading Qt documentation about QObject and qobject_cast :
http://doc.qt.io/qt-5/qobject.html#qobject_cast
I have some Qt application with QPlainTextEdit in Tab widget. When try to make a pointer on it
QPlainTextEdit *w = (QPlainTextEdit*)ui->tabWidget->widget(0)
and call a document() method
w->document()
I get a segfault.
But if i call document directly, e.g. ui->mainEdit->document(), then everything works fine.
Can anybody explain me why it happens?
You want to do:
QPlainTextEdit *w = ui->mainEdit;
Then w->document() will return what you want. You are getting the segmentation fault because when you cast ui->tabWidget->widget(0); gives a pointer to a tab page object. When you cast this to QPlainTextEdit* are telling your program to treat a part of memory that does not represent a QPlainTextEdit as a QPlainTextEdit. This causes trouble at the time that you call w->document() because that is in the memory location that it tries to access is not what it would expect from memory which belongs to QPlainTextEdit.
i'm almost sure, that ui->tabWidget->widget(0) return container widget inside of tabWidget. Try qDebug() << ui->tabWidget->widget(0)->metaObject()->className() and see what is printed. It's probably just "QWidget" not "QPlainTextEdit". Your edit is inside of layout of this widget
You can use qobject_cast to make sure that it returns the right type.
QPlainTextEdit *w = qobject_cast<QPlainTextEdit*>(ui->tabWidget->widget(0));
if (w)
{
...
}
It'll return 0 if the type is not of QPlainTextEdit*.
As stated, widget(0) is probably not returning what you wanted - and probably contains a container or some other item, and is probably not the way you want to be accessing your widgets unless there is no other way.