Qt textBrowser string display with checkbox overwrite problem - qt

when i check other box display to textbrowser first string disappear and second string displayed. How can I print without losing each other?

I guess you can use void QTextEdit::append(const QString &text) (from base class QTextEdit, see https://doc.qt.io/qt-6/qtextedit.html#append) instead of void QTextEdit::setText(const QString &text).

Related

How do I modify string from clipboard before paste in a TextFIeld?

The TextField type (I'm using QML, but shouldn't matter) provides a paste() method. However, I would like to be able to strip all white space from the clipboard data before the paste happens.
There doesn't seem to be any hooks provided by Qt to do this in a simple fashion. Does anyone know how to achieve this?
You can expose a C++ function that modifies the clipboard data and returns it to QML:
class ClipboardModifier : public QObject
{
// ...
Q_INVOKABLE QString modifiedClipboardText() const
{
QClipboard *clipboard = QGuiApplication::clipboard();
QString clipboardText = clipboard->text();
// modify the text...
return clipboardText;
}
}
Or just make a simple wrapper of QClipboard as shown in the answers here, and do the modification in QML with JavaScript and then set the text on the text field directly.

How to override all widgit tooltips to have them displayed in a single QTextBrowser widget?

I am new to Qt and therefore do not know all the in's and out's and would therefore like to know how, when you hover your mouse over a checkbox or other field that contains a tooltip, to get that tooltip text and have it applied/displayed in a QTextBrowser widget??
Thanks for any help on this issue.
Each QWidget has a "toolTip" property. To get it you can simply call:
QString toolTip = desiredWidget->toolTip();
Also, as you see, to get a toolTip as a string you don't have to wait until your mouse will hover over the desired widget. After that you can use this toolTip as you want (e.g. display it in QTextBrowser as you wrote in your question).
If you try to dynamically display tooltip of the widget under cursor, try to track mouse movement.
class MyWidget: public SuperclassWidget
{...};
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
const QWidget *widget = childAt(event->pos());
if (widget != NULL)
_textBrowser->setHtml(widget->toolTip());
SuperclassWidget::mouseMoveEvent(event);
}
There may be more clever things to prevent too frequent setting of the same tooltip, e.g. remembering the last widget.

How to associate the selected item of Combobox to write into a file

Basically I have a line-edit box which take the user input such as comma separated values and on clicking the push-button it write all the values of line-edit box to a text file. But same feature I want to achieve through combobox. So whenever one of the item selected from combobox it should write the contents of line-edit box to a text file.
The code I have used so far to implement using push-button
void MainWindow::writefile()
{
QString str = ui->lineEdit->text();
QString filename = "data.txt";
QFile file(filename);
if(file.open(QIODevice::WriteOnly|QIODevice::Text))
{
QTextStream out(&file);
out<<str<<endl;
file.close();
}
}
void MainWindow::on_pushButton_clicked()
{
writefile();
}
Sounds like all you need to do is implement ItemListener and make itemStateChanged do exactly what your actionPerformed does.

qtableview text edit

I'm writing a program with PyQt. I use QTableView to display data.
The problem is when I trigger the edit(for example press F2) of a cell, the text in the cell is all selected(highlighted) by default. It's not convenience because I want to modify the text but not rewrite them all.
So I want to know if there is any function to change the behavior?
Thank you
Not sure if there is a simpler way, but you can write your own item delegate which creates a QLineEdit. When updating the editor with model's data you deselect the text and possibly move the cursor to the beginning. The delegate would be something like this (I don't have a Qt installation available right now so I can't test it, but the idea should work):
QWidget * MyDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & option,
const QModelIndex & index) const
{
// Just creates a plain line edit.
QLineEdit *editor = new QLineEdit(parent);
return editor;
}
void MyDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
// Fetch current data from model.
QString value = index.model()->data(index, Qt::EditRole).toString();
// Set line edit text to current data.
QLineEdit * lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(value);
// Deselect text.
lineEdit->deselect();
// Move the cursor to the beginning.
lineEdit->setCursorPosition(0);
}
void MyDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const
{
// Set the model data with the text in line edit.
QLineEdit * lineEdit = static_cast<QLineEdit*>(editor);
QString value = lineEdit.text();
model->setData(index, value, Qt::EditRole);
}
If you haven't used delegates before in Qt documentation there is a useful example.
You'll need to implement a delegate so you can override the widget used for edits to that field to use a custom editor widget.
The QTableView will use a QTextEdit by default and you could try sub-classing that and altering it's behaviour. My best guess would be that you need to manipulate the focus policy on the editor widget, possibly focusInEvent[1], to change it's behaviour when it receives focus.
[1] http://doc.qt.nokia.com/4.7/qwidget.html#focusInEvent

Simple way to get all visible items in the QListView

I am trying to develop an image gallery application using Qt Framework. The application loads all the images from the selected folder and those images are displayed using QListView control.
But now i want to reduce the memory consumption by loading only the images that are visible to user. Since there is no direct function to get all the visible items in the view, i am not able to achieve this.
You can get the visible items of a list view using the indexAt function. For more details and an example you can check the following article:
http://qt-project.org/faq/answer/how_can_i_get_hold_of_all_of_the_visible_items_in_my_qlistview
I found it! You have to connect the vertical scrollbar of the listwidget to a signal:
connect(ui->listWidget->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(launch_timer()));
Every time the user scrolls, the valuechanged(int) signal is being omitted! The thing is that you shouldn't run the code provided by webclectic in this question every time the value of the vertical scrollbar of the listwidget changes, because the program will be unresponsive with so much code to run in so little time.
So, you have to have a singleshot timer and point it to the function that webclectic posted above. When launch_timer() is called, you do something like this:
if(timer->isActive()){
timer->stop();
timer->start(300);
}
else
timer->start(300);
and the timeout() signal of timer will be connected to the slot webclectic talked about. This way, if the user scrolls quickly all the way down only the last items will be updated. Generally, it will be updated anything visible for more than 300 milliseconds!
I think what you need is to implement your own model (take a look to the QAbstractListModel documentation) so that way you could decide when you have to load more images to show and maybe free some of the images that became non-visible.
although this is not so simple in Qt 4 but
it is always simple to copy below:
#include <private/qlistview_p.h>
class QListViewHelper : public QListView
{
typedef QListView super;
inline QListViewHelper() {} //not intended to be constructed
public:
inline static QVector<QModelIndex> indexFromRect(const QListView *view,
const QRect &rect)
{
const QListViewPrivate *d = static_cast<const QListViewPrivate *>(QObjectPrivate::get(view)); //to access "QListViewPrivate::intersectingSet(...)"
const QListViewHelper *helper = static_cast<const QListViewHelper *>(view); //to access "QListView::horizontalOffset()"
return d->intersectingSet(rect.translated(helper->horizontalOffset(), helper->verticalOffset()), false);
}
inline static QVector<QModelIndex> visibleItems(const QListView *view)
{ return indexFromRect(view, view->rect()); }
inline static QModelIndex firstVisible(const QListView *view)
{ return visibleItems(view).value(0); }
inline static QModelIndex lastVisible(const QListView *view) {
const QVector<QModelIndex> &items = visibleItems(view);
return items.value(items.count() - 1);
}
};
void ourTest(const QListView *view) {
QModelIndex &index = QListViewHelper::firstVisible(view);
qDebug("QListViewHelper: first visible row is %d", index.row());
index = QListViewHelper::lastVisible(view);
qDebug("QListViewHelper: last visible row is %d", index.row());
}
usage:
QModelIndex &index =
QListViewHelper::firstVisible(listViewPointerHere)
note: since it does use Qt 4.8 private-headers it may no longer work in latter versions and will need some changes.
You can keep track of all the elements that are drawn per paint event. I used a delegate and overloaded the paint event.
I also overloaded the paint event in the view. During this call, all the visible delegates will get a paint event.
If you just need to know if an item is visible, you can increment a frame count in view->paintEvent and set that number in the delegate item. The item is visible of the item matches the current frame number.
If you need a list of all visible items, clear the visible item list in view->paintEvent and add each item in the int the delegate->paintEvent to the visible items list.

Resources