I'm using QT. I want to set accessible name for a QGraphicsScene because at the mouse click I want to check whether witch graphics scene has been clicked?
Thank You
Each class that is derived from QObject(including QGraphicsScene) has following function to set a name for it:
void setObjectName ( const QString & name )
And you can retrieve this name with this function:
QString objectName () const
Related
I'm currently calling a stored proc in Oracle to populate a QComboBox in PYQT. The data appears like this:
code: 'PAY_COMP'
description: 'Payment Company'
code: 'USER_COMP'
description: 'User Company'
I want to show the description in the combobox but want to use the code as the index, is this possible? Reason is, when user selects 'Payment Company', I want to send 'PAY_COMP' to the backend to be updated.
Or is there another way to implement this?
I don't know about python, but at least in C++ you can attach QVariant data to each element in the combo box. A QVariant can be virtually every type, for example a string or an enum.
When populating the combo box I would use the member function void QComboBox::addItem(const QString & text, const QVariant & userData = QVariant()). Then when an item is selected by the user and I know the current index of the combo box, I can use QVariant QComboBox::itemData(int index, int role = Qt::UserRole) const in order to obtain the QVariant of that item, which can then be converted to the actual type the contained data has, for example using QString QVariant::toString() const.
The combo box also provides a method for obtaining the index of one particular data item: int QComboBox::findData(const QVariant & data, int role = Qt::UserRole, Qt::MatchFlags flags = static_cast<Qt::MatchFlags> ( Qt::MatchExactly | Qt::MatchCaseSensitive )) const
Now you'll only have to transfer that to python, but I assume the interface will be the same.
debug mode image
Can anyone help me reading the text from QByteArray as iam new to Qt.
Also its a internal drag and drop from QTreeWidget to a QFrame. How
can i get the QTreeWidgetItem if i am dragging the item from
Qtrewidgetitem to QFrame in dropEvent function?
void DragWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("application/x-
abstractitemmodeldatalist")) {
QByteArray i
temData = event->mimeData()->data("application/x-
abstractitemmodeldatalist");
//how to read the string from itemData//mystring is 'LDA'
QByteArray encapsulates chars. Either access them directly using [] operator or use data or constData member functions that return a (const) pointer to c char array.
I am building a notepad and want to count the words in a dialog.
QString input = ui->textEdit->toPlainText();
int spaces = input.count(" ");
ui->NumWordsLabel->setNum(spaces);
This is my attempt so far.
However, I want to execute this code in my dialog so I need to pass the
ui->textEdit->toPlainText()
Into my dialog.
This is how I create my dialog...
void MainWindow::on_actionWord_Count_triggered()
{
word_count = new Word_count();
word_count->show();
}
How would I get the required information into the dialog?
Thanks.
Generally you can pass constructor arguments to pass data to your classes. For example:
Header file:
class Word_count : public QDialog
{
Q_OBJECT
public:
explicit Word_count(QString text, QObject *parent = 0);
...
}
Source file:
Word_count(QString text, QObject *parent)
: QDialog(parent)
{
ui->setup(this);
... figure out word count and set labels ...
}
How to use:
void MainWindow::on_actionWord_Count_triggered()
{
word_count = new Word_count(ui->textEdit->toPlainText());
word_count->show();
}
Important notes:
The QObject *parent argument should always be present in the constructor arguments. Make sure to only place the = 0 in the header file, or else you will get an error.
Your constructor should be marked explicit, unless you know you do not want that. Explicit prevents the C++ compiler from automatically casting to your type using a given constructor.
Pass the parent parameter to your inheriting class, whether that be QDialog, QWidget or QObject, using the constructor initializer list syntax. This is done in the source file example with : QDialog(parent).
You can add as many arguments as you need, but they should be before the parent argument. This is because the parent argument has a default value that can be implied. Because you must specify arguments in order, it can not be implied if there are required parameters after it.
This only will work for creating the dialog. If you want the dialog to dynamically update, you'll need to use a slot or method like suggested by others. Alternatively, if you don't want a dynamically updating dialog, consider using exec instead of show so that users must close your word count dialog before continuing with their work.
Add a slot like void setText( const QString& text ) to your Word_count class.
Then, you can emit a signal like void textChanged( const QString& text ) const from your MainWindowclass.
Don't forget to connect both.
I started my first steps programming in Qt and can't find an answer.
I have some widget, let's say it's named "tab".
I want to add to it's layout new widget.
I can do it by writing:
QLabel *label = new QLabel(tab);
I want to do the same thing , but instead of saying "tab" I want to use widget name stored in QString variable. Something like this (but it doesn't work, can't convert QString to QWidget):
QString name = "tab";
QLabel *label = new label(zakladka);
Is it possible?
QObjects have a property objectName. So if you name your objects and have them be part of a hierarchy of QObject (i.e. you know they are children of a parent object) you can make use of findChild
QLabel* label = new QLabel(parentWidget->findChild(name));
I'm trying to use a delegate to customize the way data from a model is displayed when using a QDataWidgetMapper.
I have two different versions of a widget, one is view-only (the data is displayed in QLabels) and the other is used to edit the data (the data is displayed in appropriate editors).
The latter one works perfectly with the delegate, everything is fine.
As you may have guessed the problem arises with the first one... When mapping the sections of my model to QLabels using the QDataWidgetMapper, the delegate is never called and the data is displayed correctly for the sections with regular data (strings, ints,...) but no data is displayed for the sections of my model with a custom data type (a kind of list) which I would like to format as a string using the delegate.
I've already performed this operation successfully when the same data is displayed in a QTableView (the method paint() of the delegate is called when the data is displayed).
After having looked at it a little bit closer, I've been able to see that, when using QLabels to display the data, the delegate is never called though I've explicitly associated a delegate to the QDataWidgetMapper using its method setItemDelegate().
So in synthesis, assume a class CustomItemDelegate which inherits QStyledItemDelegate with virtual methods:
void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
qDebug() << "DELEGATE: PAINT" << index.column();
QStyledItemDelegate::paint(painter, option, index);
}
void CustomItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
qDebug() << "DELEGATE: SET EDITOR DATA" << index.column();
QStyledItemDelegate::setEditorData(editor, index);
}
and a widget with following in it:
QDataWidgetMapper* mapper = new QDataWidgetMapper();
CustomItemDelegate* delegate = new CustomItemDelegate();
mapper->setModel(model);
mapper->setItemDelegate(delegate);
mapper->addMapping(editorWidget, 1);
mapper->addMapping(label, 2, "text");
mapper->toFirst();
QTableView* view = new QTableView();
CustomItemDelegate* delegate2 = new CustomItemDelegate();
view->setModel(model);
view->setItemDelegate(delegate2);
the code outputs:
DELEGATE: SET EDITOR DATA 1
// NOTHING ?!
DELEGATE: PAINT 1
DELEGATE: PAINT 2
and as a result I got
my editorWidget with the correct data in it (whatever data type the section contains: regular or custom, as long as the editor handles the type of course),
my label only displays the data if the section contains a regular type of the data as the delegate is not called
my view would display everything fine as the delegate is called for each section
So my questions are:
why isn't the delegate called when the mapped widget is a QLabel?
in this case, how come the data is even displayed when the data type is regular? Magic?
Thanks very much and I apologize in advance if the answer is obvious (but even then, thank you for pointing it out :P),
ixM
This is the code from QT that populates widgets
void QDataWidgetMapperPrivate::populate(WidgetMapper &m)
{
if (m.widget.isNull())
return;
m.currentIndex = indexAt(m.section);
if (m.property.isEmpty())
delegate->setEditorData(m.widget, m.currentIndex);
else
m.widget->setProperty(m.property, m.currentIndex.data(Qt::EditRole));
}
In the first case when you do not specify a property delegate is used whereas in the second case the data is set to widget directly by passing your delegate.
I don't know why it was designed this way but this is how it works currently !