QT: Qtableview wrapping text - qt

I am working with QtableView of Qt. I am facing one problem. I am not able to fix the content in to complete cell . In my case i have fix size of column and rows can be stretched. following is sample code
#include <QApplication>
#include"QStandardItemModel"
#include"QTableView"
#include"QStandardItem"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStandardItemModel *model = new QStandardItemModel(8,3);
QTableView * pQTableView = new QTableView();
for(int r = 0;r<8;r++)
for(int c = 0; c<3; c++)
{
QModelIndex index1= model->index(r,c);
QVariant value("Swaminarayan maharaj");
model->setData(index1, value,Qt::DisplayRole );
QVariant value1( Qt::AlignCenter);
model->setData(index1, value1,Qt::TextAlignmentRole );
}
pQTableView->resize(400,400);
pQTableView->setModel(model);
pQTableView->setColumnWidth(0, 100);
pQTableView->setColumnWidth(1, 100);
pQTableView->setColumnWidth(2, 100);
pQTableView->show();
return a.exec();
}
As you can see i wants each cell to have "Swaminarayan maharaj".But Swaminarayan shold be in first line of the cell and "maharaj" in second line. Inshort each cell shold display content in two lines.
In folllowing link i found Qt::TextWordWrap but i am not able to use in my code
http://www.qtcentre.org/threads/27839-For-Qt-4-6-x-how-to-auto-size-text-to-fit-in-a-specified-width

Related

How to exclude one row from sort by column on QSortFilterProxyModel

I'm using QTableView with QSortFilterProxyModel.
I have emty row for manually input:
Example of table
Now i want to sort by Date
But i don't wanna move the last row
Example of sort table
I will be grateful for the help
proxyModel->sort(2);
You can use a custom QSortFilterProxyModel that does not sort the last row. In the following example you will see on the left side the original table and on the right side the table with the special order.
#include <QtWidgets>
class SortFilterProxyModel: public QSortFilterProxyModel
{
public:
using QSortFilterProxyModel::QSortFilterProxyModel;
bool lessThan(const QModelIndex &left,
const QModelIndex &right) const
{
int row_max = std::max(left.row(), right.row());
if(row_max >= sourceModel()->rowCount()-1)
return left.row() < right.row();
return QSortFilterProxyModel::lessThan(left, right);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QTableView *left_view = new QTableView;
QTableView *right_view = new QTableView;
QHBoxLayout *hlay = new QHBoxLayout(&w);
hlay->addWidget(left_view);
hlay->addWidget(right_view);
QStandardItemModel model(12, 3);
SortFilterProxyModel proxy;
proxy.setSourceModel(&model);
left_view->setModel(&model);
right_view->setModel(&proxy);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(100.0, 200.0);
for (int i=0; i< model.rowCount(); ++i) {
for(int j=0; j<model.columnCount(); ++j){
QStandardItem *it = new QStandardItem;
it->setData(dis(gen), Qt::DisplayRole);
model.setItem(i, j, it);
}
}
w.resize(640, 480);
proxy.sort(2);
w.show();
return a.exec();
}

Undesirable text overlapping

When I edit the QTableView the old text is not cleared and so the new text overlaps it. How can I avoid this behaviour?
The code:
#include <QApplication>
#include <QtSql>
#include <QtGui>
#include <QTableView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE");
db1.setDatabaseName(":memory:");
db1.open();
QSqlQuery("CREATE TABLE test (a integer primary key, s text)");
QSqlQuery("INSERT INTO test VALUES (1, 'aaa');");
QSqlTableModel *model = new QSqlTableModel(0, db1);
model->setTable("test");
model->select();
QTableView *view = new QTableView;
view->setModel(model);
view->show();
return a.exec();
}
I have simular issue with dynamic QLabel.
When label text is updated new text was overlaped with old one. The problem was related to transparent background color.
As you find out the solution for you is to use such stylesheet QTableView::item {}
Complete Code:
view->setStyleSheet("QTableView::item {}");

Remove/delete/replace selected text in QGraphicsTextItem

I would like to delete the selected text inside a QGraphicsTextItem.
I have been searching all the classes it uses - like QTextCursor, QTextDocument... I can't find anything to remove text, except the clear() function of the QTextDocument which removes everything...
How can I remove the selection ?
QTextCursor _cursor = textCursor();
if(_cursor.hasSelection())
?
Alternatively (since I need this for a custom paste command), how can I replace the selection with an existing text or html ?
QClipboard* _clipboard = QApplication::clipboard();
const QMimeData* _mimeData = _clipboard->mimeData();
if (_mimeData->hasHtml())
{
QTextCursor _cursor = textCursor();
if(_cursor.hasSelection())
?
_cursor.insertHtml(_mimeData->html());
}
Is not working QTextCursor::removeSelectedText()?
In the next example, we have at the beginning the text QGraphics Text Item 1, but as you will see, we can get the QTextDocument and also the QTextCursor for that document and insert some words.
After that, we move the cursor to the next word. Finally, we select the word under the cursor (Text) and remove it from our QGraphicsTextItem.
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsTextItem>
#include <QTextCursor>
#include <QTextDocument>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsTextItem* item_1 = new QGraphicsTextItem("QGraphics Text Item 1");
item_1->setTextInteractionFlags(Qt::TextEditorInteraction);
QTextDocument* doc = item_1->document();
scene.addItem(item_1);
QTextCursor cursor(doc);
cursor.beginEditBlock();
cursor.insertText(" Hello ");
cursor.insertText(" World ");
cursor.endEditBlock();
cursor.movePosition(QTextCursor::NextWord);
cursor.select(QTextCursor::WordUnderCursor);
cursor.removeSelectedText();
view.setFixedSize(640, 480);
view.show();
return a.exec();
}

convert GIcon to QIcon

Is there a way to convert between these datatypes? I'm working with Qt but still need some of glib capabilities and I haven't found a way to do this. I need to get a list of the installed applications with GAppInfo and show it in a QListView and to do so I need to get the icon for those applications. Extracting it using g_app_info_get_icon returns a GIcon and what I need to work with is a QIcon in order to get it's QVariant.
GIcon does not provide the actual pixmap for the icon.
You need to load an actual pixmap by requesting icon of certain size.
#include <gio/gdesktopappinfo.h>
#include <gtk/gtk.h>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QIcon icon;
GAppInfo *appInfo = (GAppInfo *)g_desktop_app_info_new("vlc.desktop");
GIcon *gicon = g_app_info_get_icon(appInfo);
QList<int> sizes; sizes << 16 << 24 << 32 << 48 << 64;
foreach (int size, sizes) {
GtkIconInfo *iconInfo = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), gicon, size, (GtkIconLookupFlags)0);
GdkPixbuf *pixbuf = gtk_icon_info_load_icon(iconInfo, NULL);
if (pixbuf == NULL)
continue;
QImage image(
gdk_pixbuf_get_pixels(pixbuf),
gdk_pixbuf_get_width(pixbuf),
gdk_pixbuf_get_height(pixbuf),
gdk_pixbuf_get_rowstride(pixbuf),
QImage::Format_ARGB32);
g_object_unref(pixbuf);
image.save("icon-" + QString::number(size) + ".png");
icon.addPixmap(QPixmap::fromImage(image));
}
g_object_unref(gicon);
return 0;
}

img viewer with Qt

I'm trying to create an application based on model/view concept.
i need to open some directory, find all imgs in it and show them in MainWindow (subclass of QMainWindow).
The architecture is something like this:
1) via QDir create QStringList of "good" file names (using file names filter by extentions).
2) create QStandardItemModel and fill it with QStandardItem (QIcon(QImage(fileName).scaled(QSize)), fileName).
3) use QListView to show data from the model.
but there is some problems.
first of all - theModel.columnCount is, e.g., 52 but only one picture is shown on the screen and without its name.
can someone help me:
1) how to fill model correctly? my approach:
QDir dirs(dir);
QStringList imgs = dirs.entryList(QStringList() << "*.jpg" << "*.jpeg" << "*.bmp" << "*.png");
itemModel->clear();
QList<QStandardItem *> listItem;
for(int i = 0; i < imgs.count(); ++i){
QImage image = QImage(dir + "/" + imgs.at(i)).scaled(QSize(size().width()/4, size().height()/4));
QStandardItem *item = new QStandardItem();
item->setIcon(QIcon(QPixmap::fromImage(image)));
item->setData(imgs.at(i));
listItem << item;
}
itemModel->appendRow(listItem);
this code is in one slot of the MainWindow class.
2) as I understand, my view is automatically updated, so it should show all data from the model.
am I right, or some code is necessary?
3) maybe I haven't done somethings in initialization of the model and the view (the code is in te constructor of class MainWindow):
itemModel = new QStandardItemModel(this);
listView = new QListView(this);
listView->setModel(itemModel);
// listView->setFlow(QListView::LeftToRight);
// listView->setLayoutMode(QListView::Batched);
listView->setViewMode(QListView::IconMode);
listView->setResizeMode(QListView::Adjust);
// listView->setGridSize(QSize(size().width()/4, size().height()/4));
listView->setIconSize(QSize(size().width()/4, size().height()/4));
setCentralWidget(listView);
Since you determined that you needed appendColumn the last bit would be to add the QIcon as data with the Qt::DecorationRole. The follow works for me for viewing images in the same folder the program is run (though I don't know why it is shown with a grid layout).
#include <QApplication>
#include <QStandardItemModel>
#include <QListView>
#include <QDir>
#include <QStringList>
#include <QList>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStandardItemModel* itemModel = new QStandardItemModel();
QListView* listView = new QListView();
QDir dirs(".");
QStringList imgs = dirs.entryList(QStringList() << "*.jpg" << "*.jpeg" << "*.bmp" << "*.png");
QList<QStandardItem *> listItem;
for(int i = 0; i < imgs.count(); ++i){
QImage image = QImage(dirs.absoluteFilePath(imgs.at(i))).scaled(QSize(80, 60));
QStandardItem *item = new QStandardItem();
item->setData(QVariant(QPixmap::fromImage(image)), Qt::DecorationRole);
listItem << item;
}
itemModel->appendColumn(listItem);
listView->setModel(itemModel);
listView->setViewMode(QListView::IconMode);
listView->show();
a.exec();
}

Resources