I've got a QComboBox which I want to be "automatically" editable. That is, every time a user manually changes current item's text, that text should "fall" to the underlying model automatically.
So far, I've reached this via a custom signal handler:
void setupUi() {
...
connect( someComboBox,
SIGNAL(editTextChanged(QString)),
SLOT(comboBoxEditTextChanged(QString)) );
...
}
void comboBoxEditTextChanged( const QString& text ) {
someComboBox->setItemText( someComboBox->currentIndex(), text );
}
So I wonder, is there a possibility to do this with less code? I've tried QComboBox::setInsertPolicy(QComboBox::InsertAtCurrent), but that didn't help.
EDIT: Current method with a custom slot works properly - but I'm asking if there's a method that does not involve any signals/slots.
To set the Text Automatically when USER changes it, we can edit your slot as follows:
void comboBoxEditTextChanged( const QString& text )
{
int index = someComboBox->findText(text);
if(index != -1)
{
someComboBox->setCurrentIndex(index);
}
someComboBox->setItemText( someComboBox->currentIndex(), text );
}
I hope this will resolve your issue
QComboBox can add items manually using
combo->additem("X");
combo->addItem(QString Y);
whereas you can manage the maximum number of items in it. Please go through the following link for details.
a link
So, in your slot,
void comboBoxEditTextChanged( const QString& text )
{
someComboBox->addItem(text);
}
Related
I have a QTextEdit whose textFormat is Qt::RichText so it is possible to format the text with HTML tags. On this QTextEdit I have a QPopupMenu thats filled with QActions. One of these actions is a simple copy that connects into SLOT( onClipboardCopy() ).
QTextEdit's copy() is defined as "Copies any selected text (from selection 0) to the clipboard."
If there is something selected this function is perfect. However, I'd like to copy ALL of the TextEdit's content when nothing is selected.
Here's the slot:
void WidgetName::onClipboardCopy()
{
if ( TextEdit->hasSelectedText() )
{
TextEdit->copy();
}
else
{
QClipboard * xClipboard = QApplication::clipboard();
xClipboard->setText( TextEdit->text() );
}
}
The problem is in else TextEdit->text() returns the text with all of it's HTML tags. Is there an easy way to discard them?
Okay, I realized that I could select the text before copying. I get the desired effect by changing the code to:
void WidgetName::onClipboardCopy()
{
if ( TextEdit->hasSelectedText() )
{
TextEdit->copy();
}
else
{
TextEdit->selectAll();
TextEdit->copy();
TextEdit->removeSelection();
//QClipboard * xClipboard = QApplication::clipboard();
//xClipboard->setText( TextEdit->text() );
}
}
I have a QTextEdit that contains a QTextDocument, which is being programatically edited using the QTextCursor interface. The document is being edited with QTextCursor::insertText().
I load the text file being edited in chunks, so the initial size of the QTextDocument might only be 20 lines even though the document is 100,000 lines. However, I want the QTextEdit scrollbar to reflect the full size of the document instead of just the 20 line document it's currently displaying.
The QTextEdit's scrollbar range is set with QScrollBar::setMaximum() which adjusts the scrollbar to the proper size on the initial opening of the file, but when QTextCursor::insertText() is called the QScrollBar's range is recalculated.
I've already tried calling QScrollBar::setMaximum() after each QTextCursor::insertText() event, but it just makes the whole UI jerky and sloppy.
Is there any way to keep the range of the QScrollBar while the QTextDocument is being modified?
Yes. You'd depend on the implementation detail. In QTextEditPrivate::init(), the following connection is made:
Q_Q(QTextEdit);
control = new QTextEditControl(q);
...
QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), q, SLOT(_q_adjustScrollbars()))
Here, q is of the type QTextEdit* and is the Q-pointer to the API object. Thus, you'd need to disconnect this connection, and manage the scroll bars on your own:
bool isBaseOf(const QByteArray &className, const QMetaObject *mo) {
while (mo) {
if (mo->className() == className)
return true;
mo = mo->superClass();
}
return false;
}
bool setScrollbarAdjustmentsEnabled(QTextEdit *ed, bool enable) {
QObject *control = {};
for (auto *ctl : ed->children()) {
if (isBaseOf("QWidgetTextControl", ctl->metaObject()) {
Q_ASSERT(!control);
control = ctl;
}
}
if (!control)
return false;
if (enable)
return QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()), Qt::UniqueConnection);
else
return QObject::disconnect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()));
}
Hopefully, this should be enough to prevent QTextEdit from interfering with you.
I wave a QListView that is backed by a QStandardItemModel. Under certain circonstances, the QStandardItem are made checkable. A checkbox gets displayed besides the item's display. At some point, I want to remove hide the QStandardItem checkbox. I set its checkable state to false but it doesn't hide the checkbox (though it cannot be checked anymore).
The only way I have found of hiding the checkbox is to replace the item with a new one. This doesn't seem the proper way to preceed.
This is the code:
MyModel::MyModel(QObject *parent):QStandardItemModel(parent){}
void MyModel::createItem(int row, const QString &text)
{
setItem(row, new QStandardItem(text));
}
void MyModel::setCheckable(int row)
{
item(row)->setCheckState(Qt::Unchecked);
item(row)->setCheckable(true); // A checkbox appears besides the text
}
void MyModel::hideCheckBox(int row)
{
item(row)->setCheckState(Qt::Unchecked);
item(row)->setCheckable(false); // does not work
// I need to completely replace the item for the checkbox to disapear.
// This doesn't seem the proper way to proceed
setItem(row, new QStandardItem(item(row)->data(Qt::DisplayRole).toString()));
}
Is there better way to proceed?
When you call setCheckState or setCheckable, the qt will update the data of list item by adding or setting a Qt::CheckStateRole data. If the Qt::CheckStateRole data is existed, the check icon will be shown. So you need remove it from the data map of the list item.
Finally, the code of hideCheckBox should be:
void MyModel::hideCheckBox(int row)
{
// check the item pointer
QStandardItem* pitem = item(row);
if (pitem == NULL) return;
// find and delete the Qt::CheckStateRole data
QMap<int, QVariant> mdata = itemData(pitem->index());
if (mdata.remove(Qt::CheckStateRole))
{
setItemData(pitem->index(), mdata);
}
}
Hope it useful. :)
I think the presence of the check boxes in items defined by item flags, so that I would write the function in the following way:
void MyModel::hideCheckBox(int row)
{
// Does not set the Qt::ItemIsUserCheckable flag.
item(row)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
I'm looking for a clean and cross-platform way to Prevent dropping an item FROM a Qt application TO Windows File Explorer (or other OS equiv.)
The following diagram shows the desired behavior:
I haven't had luck finding examples online or hacking a work-around together, but it seems like it would be a common-enough use-case that there would be a well designed and implemented solution floating around.
What I've tried and do not have working:
Detecting the Drag and Killing It:
detecting the QDragEnterEvent, QDragMoveEvent, QDragLeaveEvent
comparing the answerRect() or pos() of the event to the Geometry of
the Window or Widget to detect if the drag has left the application
This is pretty hacky (and not working at them moment) and I'm hoping you can point me towards a more elegant solution.
(UPDATE - tried changing mimeType, but Windows File Explorer still accepts the drop)
Changing the MIME Type to a custom type:
Pre: the "Widget w/ Drag & Drop" from the diagram above is a QTreeView with a QFileSystemModel model
Sub-classing the QFileSystemModel and overriding the mimeTypes() function like the code below
From the qDebug() output, it looks like the mimeType is correctly being set, but Windows File Explorer still accepts the drop :/
QStringList MyFileSystemModel::mimeTypes() const
{
QStringList customMimeTypes;
customMimeTypes << QString("UnicornsAndRainbows/uri-list");
qDebug() << "customMimeTypes: " << customMimeTypes;
return customMimeTypes;
}
Please let me know when you have a chance.
Thanks! :)
Dmitry Sazonov gave the correct answer. I will explain how I implemented it below. Dmitry, if you want cred, post it as an answer and not a comment so I can accept it as the answer.
What I did wrong on my question update based on Dmitry's suggestion was to override the QFileSystemModel::mimeTypes() when, in fact, I had to modify the QTreeView::mouseMoveEvent() and QTreeView::dropEvent().
//---------------------------------------------------------
void MyTreeView::mouseMoveEvent( QMouseEvent *event )
{
if( !(event->buttons() & Qt::LeftButton) )
{
return; // we only care about left mouse drags at the moment
}
if( (event->pos() - dragStartPosition).manhattanLength() < QApplication::startDragDistance() )
{
return; // a buffer when calculating waht qualifies as a "drag event"
}
QDrag *drag = new QDrag( this );
QMimeData *mimeData = new QMimeData();
QByteArray data;
const QStringList selectedPaths = this->getSelectedPaths(); // custom helper method
foreach( QString path, selectedPaths )
{
data.append( path ).append( ";" ); // using ';' as path deliminator
}
data.chop( 1 );
//--- this sets the custom MIME Type filter
mimeData->setData( CUSTOM_MIMETYPE_STRING, data );
drag->setMimeData( mimeData );
Qt::DropAction dropAction = drag->exec( Qt::CopyAction );
}
//---------------------------------------------------------
void MyTreeView::dropEvent( QDropEvent *event )
{
// ...
QList<QByteArray> paths;
//--- this filters based on our custom MIME Type
paths = event->mimeData()->data( CUSTOM_MIMETYPE_STRING ).split(';');
foreach( QByteArray path, paths )
{
// do something with the file paths
}
}
I have a treewidget in my Qt form. It shows a tree of files, showing a icon representing something about them, and their name.
I entered these using treeItem->setIcon(0, *icon), and treeItem->setText(0, text) .
The reason I entered both values to the same column (0), is because otherwise the icons would not stay next to the text, rather always stick to the left, even when the text was indented to the right (because it's a child of another item).
The problem is, now I can't tell if the user clicked on the icon or on the text itself, and I need to deal with these separately.
So, is there anyway to get more info than just the treeitem and column when an object in a treewidget is clicked,
or is there any way to put them on seperate columns and still have the normal behavior icons and text should have?
Thanks.
I don't think there is a straight forward way to get more info, if you are simply using the clicked() or itemClicked() signals. You probably have to create a custom class that inherits QTreeWidget, and reimplement one of the virtual mouse-event functions.
mouseMoveEvent ( QMouseEvent * )
mousePressEvent ( QMouseEvent * )
mouseReleaseEvent ( QMouseEvent * )
This is not something I would recommend, unless you really know what you are doing, and really need to do it.
However, I can't remember seeing a list widget anywhere, where clicking an icon is handled differently from clicking the text in the same column. So if you are looking for "the normal behavior icons and text should have", you probably should look for another solution.
I found the following solution for this problem:
void MyTreeWidget::mousePressEvent( QMouseEvent* aEvent )
{
QTreeWidget::mousePressEvent( aEvent );
const QPoint clickedPosition = aEvent->pos();
const QRect itemRectangle = visualItemRect( itemAt( clickedPosition ) );
const int iconOffset = itemRectangle.height() - iconSize().height();
QRect iconRectangle;
iconRectangle.setTopLeft( itemRectangle.topLeft() + QPoint( iconOffset, iconOffset ) );
iconRectangle.setWidth( iconSize().width() );
iconRectangle.setHeight( iconSize().height() );
if ( iconRectangle.contains( clickedPosition ) )
{
qDebug() << "ICON clicked";
// Emit an icon clicked SIGNAL.
}
}