I apologize for not doing a very good job of describing my situation, but I think you hit on the problem with
then why are you having such a hard time getting the current (application) directory and creating paths relative to that?
I am storing the path, in the database, as a string(VARCHAR), either absolute, or relative, I am leaving it to the user to select which. So, once it is entered into the database, it basically becomes an absolute path (which is why I cannot use QApplication::applicationDirPath()). For example, this is how I handle it in the model.
{
if(header == "Datasheet")
{
switch(role)
{
case Qt::DisplayRole:
break;
case Qt::DecorationRole:
if(QStringRef(&link, 0, 4) == "http")
{
QPixmap pixmap
(":/Images/Images/internet.png");
return pixmap;
}
else if(link == "")
else
{
QPixmap pixmap
(":/Images/Images/pdf.png");
return pixmap;
}
break;
case Qt::UserRole:
return link;
break;
}
}
if(header == "Image")
{
switch(role)
{
case Qt::ToolTipRole:
return QString("<img scr='%1'>").
arg(link
);
break;
}
}
}
QVariant TableModel::data(const QModelIndex &idx, int role) const
{
QString header = QSqlTableModel::headerData(idx.column(), Qt::Horizontal).toString();
if(header == "Datasheet")
{
QString link = QSqlTableModel::data(idx, Qt::DisplayRole).toString();
switch(role)
{
case Qt::DisplayRole:
return QString();
break;
case Qt::DecorationRole:
if(QStringRef(&link, 0, 4) == "http")
{
QPixmap pixmap(":/Images/Images/internet.png");
return pixmap;
}
else if(link == "")
return QVariant();
else
{
QPixmap pixmap(":/Images/Images/pdf.png");
return pixmap;
}
break;
case Qt::UserRole:
return link;
break;
}
}
if(header == "Image")
{
QString link = QSqlTableModel::data(idx, Qt::DisplayRole).toString();
switch(role)
{
case Qt::ToolTipRole:
return QString("<img scr='%1'>").arg(link);
break;
}
}
return QSqlTableModel::data(idx, role);
}
To copy to clipboard, switch view to plain text mode
And just for completeness, unless I forgot something, the PDFs are handled this way
void MainWindow
::on_dataView_activated(const QModelIndex &index
) {
QString header
= model
->headerData
(index.
column(), Qt
::Horizontal).
toString();
if(header == "Datasheet")
{
QString link
= index.
data(Qt
::UserRole).
toString();
if(QStringRef(&link, 0, 4) == "http")
else if(link == "")
return;
else
}
}
void MainWindow::on_dataView_activated(const QModelIndex &index)
{
QString header = model->headerData(index.column(), Qt::Horizontal).toString();
if(header == "Datasheet")
{
QString link = index.data(Qt::UserRole).toString();
if(QStringRef(&link, 0, 4) == "http")
QDesktopServices::openUrl(link);
else if(link == "")
return;
else
QDesktopServices::openUrl("file///" + link);
}
}
To copy to clipboard, switch view to plain text mode
Then you just have the problem of finding the database at runtime.
There is no problem using QApplication::applicationDirPath() for that, since it is not based on a string.
This has gotten me to thinking that I might be able to use similar mechanics, like I do in the last bit using QStringRef(), to determine if the link has been truncated, to a relative link, in which case I should then be able to use QApplication::applicationDirPath(). I am going to have to think on that a bit.
<edit> I expect the user to not worry about the relative path part, if they are not interested having the application portable.</edit>
Bookmarks