Mapgraphics library on github. C++/Qt/OpenStreetMap - c++

I have a question regarding this library: https://github.com/raptorswing/MapGraphics. Contact the maker did not work, so I hope for help here, can someone faced with this same problem.
How do I add an object to my map in this library? Tried to do here is so:
MapGraphicsScene *scene = new MapGraphicsScene();
MapGraphicsView *view = new MapGraphicsView(scene);
LineObject line(Position(92.860984, 56.006355), Position(92, 56), 10);
QPainter linePainter;
QColor red(255, 0, 0);
linePainter.setPen(red);
QStyleOptionGraphicsItem itm;
line.paint(&linePainter, &itm, view);
QSharedPointer<OSMTileSource> osmTiles(new OSMTileSource(OSMTileSource::OSMTiles), &QObject::deleteLater);
QSharedPointer<CompositeTileSource> composite(new CompositeTileSource(), &QObject::deleteLater);
composite->addSourceBottom(osmTiles);
view->setTileSource(composite);
But it gave no results. Can you help me? :(

Unfortunately the relevant bit of code isn't in your snippet, but I wonder if it's http vs https access? When you try and download a tile such as this http one it'll now redirect to https. This caught out a number of clients (to my knowledge Java-based and Flash-based ones).
The github project says "It is a Qt map widget that can use tiles from MapQuest, Openstreetmap, or a custom source you define" which suggests it's actually quite old, since Mapquest haven't had their own map tiles for a very long time. Try using a custom source and define https access to OSM tiles.

It is enough to do the following:
MapGraphicsObject *line = new LineObject(Position(92.860984, 56.006355), Position(92, 56), 10);
view->scene().addObject(line);
This should be done with any type of object.

Related

Application crashes when using global QNetworkCookieJar for multiple windows

I am new at Qt and using Qt5.5 to create a http client application which will load a window with a fixed URL. After login in this window the other windows of same site should get the same session. At this purpose I have used global pointer jar of QNetworkCookieJar class and implemented on the following code for every window
Window1::Window1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Window1)
{
ui->setupUi(this);
QUrl webURL("http://someURL");
ui->webView->show();
ui->webView->load(webURL);
ui->webView->page()->networkAccessManager()->setCookieJar(jar);
}
It solves the multiple authentication problem, but when I close any window and reopen it instantly it causes appcrash.
Please give me a suggestion on my problem. Thanks in advance.
From the Qt5 docs (http://doc.qt.io/qt-5/qnetworkaccessmanager.html#setCookieJar):
Note: QNetworkAccessManager takes ownership of the cookieJar object.
So the accessmanager will delete your jar instance. There might be your problem! I don't know enough about the webview/page/accessmanager to be sure about the lifetime of the manager, but it seems to be bound to the webview/your ui, so when it's closed+destroyed, your cookiejar will have gone, too.
As QNetworkCookieJar inherits from QObject, you might want to use a guarded QPointer<QNetworkCookieJar> jar instead of a simple QNetworkCookieJar* jar variable. Then, you will notice that your guarded pointer becomes null after the first window is closed/destroyed. That would a) verify my claim from the last paragraph and b) guard you against stale pointer accesses in the future (in fact, your program would no longer crash).
Thanks ThorngardSO..
I have found a solution using your answer. Here is my solution,
if(jar.isNull()==true){
qDebug()<<"object null";
QPointer <QNetworkCookieJar> jar_new = new QNetworkCookieJar(0);
ui->webView->page()->networkAccessManager()->setCookieJar(jar_new);
jar=jar_new;
}
else
ui->webView->page()->networkAccessManager()->setCookieJar(jar);

connecting signals and slots with different relations

First of all I'd say I'm a noob to GUI programming. I use Qt 5.4.
I came up with this code while watching voidRealms videos.
connect(ui->horizontalSlider,SIGNAL(sliderMoved(int)),ui->progressBar,SLOT(setValue(int)));
Obviously this connects slider movement with progressbar fill. This actually works like
progressbarfill <- slidermovement.
How can I make into a different relation? Like
progressbarfill <- (slidermovement)/2 or something like that.
You need to create new slot for that purpose.
But in C++ 11 and Qt 5 style you can use labmdas! It is very comfortable for such short functions.
In your case:
connect(ui->horizontalSlider, &QSlider::sliderMoved, this, [this](int x) {
this->ui->progressBar->setValue(x / 2);
});

Transparent Background in QWebEnginePage

We are trying to port some application from Qt 4 to Qt 5.4. The Qt 5.4 has a new web engine. We used to make the background of QWebView and QWebPage to be transparent:
view = new QWebView(this);
QPalette palette = view->palette();
palette.setBrush(QPalette::Base, Qt::transparent);
view->page()->setPalette(palette);
view->setAttribute(Qt::WA_OpaquePaintEvent, false);
But this code doesn't work for QWebEngineView and QWebEnginePage. The point is that QWebEnginePage doesn't have such an API like setPalette.
Can anyone find a way to solve this?
As mentioned in https://bugreports.qt.io/browse/QTBUG-41960, this is now working by simply using this line:
webEngineView->page()->setBackgroundColor(Qt::transparent);
I've tried it in Qt 5.6 and it works well.
Update: In order to make this answer more helpful, let me show all relevant code.
In MainWindow, I have set this:
setAttribute(Qt::WA_TranslucentBackground);
setAutoFillBackground(true);
setWindowFlags(Qt::FramelessWindowHint);
For the webEngineView object, I have set these attributes:
webEngineView->setAttribute(Qt::WA_TranslucentBackground);
webEngineView->setStyleSheet("background:transparent");
webEnginePage = webEngineView->page();
// https://bugreports.qt.io/browse/QTBUG-41960
webEnginePage->setBackgroundColor(Qt::transparent);
No. A partial solution has been committed to upstream, but it covers only QtQuick and you can't have any elements on top:
https://bugreports.qt.io/browse/QTBUG-41960

Temporarily disable touch with CCLayerPanZoom

I've recently added the CCLayerPanZoom cocos2d extension to my project and got my game scene zooming and scrolling just like I want. Now when the player takes certain actions, I want to be able to disable the pan/zoom temporarily while they perform an action but can't figure out how to do it. I searched around and found the following code in a forum but it doesn't work or I don't know how to use it.
Does anyone know how to do this properly either with different code or the code below?
-(void)enableTouches:(BOOL)enable {
if(enable) {
[[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:0];
_panZoomLayer.isTouchEnabled = YES;
CCLOG(#"LayerPanZoom enabled.");
} else {
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
_panZoomLayer.isTouchEnabled = NO;
CCLOG(#"LayerPanZoom disabled.");
}
}
I finally figured it out and figured I would post the answer back up here to share. The code I posted wasn't working because I was sending back self instead of the _panZoomLayer. So here are the steps to get this working yourself.
Implement the CCLayerPanZoom into your project as described by the documentation.
Add the following code as a method to call on your new CCLayerPanZoom class.
-(void)enableTouches:(BOOL)enable {
if(enable) {
[[CCTouchDispatcher sharedDispatcher] addStandardDelegate:_panZoomLayer priority:0];
CCLOG(#"LayerPanZoom enabled.");
} else {
[[CCTouchDispatcher sharedDispatcher] removeDelegate:_panZoomLayer];
CCLOG(#"LayerPanZoom disabled.");
}}
NOTE: Make sure to put the instance of the parent class as the delegate to remove.
In order to re-enable and have it function properly, you have to remove all the entries from the array in the CCLayerPanZoom class before calling to re-register the delegate. I created a new method in the CCLayerPanZoom class as follows and just call it right before the addStandardDelegate method above.
-(void)removeTouchesFromArray {
[self.touches removeAllObjects];
}
Then it all works great! Took me a while to learn how to use this extension but it works perfect once you figure it all out. I can single finger pan, double finger zoom/pan, set center location for entire scene, limit panning past edges, and set min/max scales. I know people have had a lot of issues with this but it is a great extension, just takes some messing around with to understand it. Let me know if you have any questions. Hope this helps someone else.

Using images in QListWidget, is this possible?

I am using QT to create a chat messenger client. To display the list of online users, I'm using a QListWidget, as created like this:
listWidget = new QListWidget(horizontalLayoutWidget);
listWidget->setObjectName("userList");
QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Expanding);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(listWidget->sizePolicy().hasHeightForWidth());
listWidget->setSizePolicy(sizePolicy1);
listWidget->setMinimumSize(QSize(30, 0));
listWidget->setMaximumSize(QSize(150, 16777215));
listWidget->setBaseSize(QSize(100, 0));
listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
Users are shown by constantly refreshing the list, like this: (Note: There are different channels, with different userlists, so refreshing it is the most efficient thing to do, as far as I know.)
void FMessenger::refreshUserlist()
{
if (currentPanel == 0)
return;
listWidget = this->findChild<QListWidget *>(QString("userList"));
listWidget->clear();
QList<FCharacter*> charList = currentPanel->charList();
QListWidgetItem* charitem = 0;
FCharacter* character;
foreach(character, charList)
{
charitem = new QListWidgetItem(character->name());
// charitem->setIcon(QIcon(":/Images/status.png"));
listWidget->addItem(charitem);
}
}
This has always worked perfectly. The line that I commented out is the one I have problems with: my current goal is to be able to display a user's online status with an image, which represents whether they are busy, away, available, etc. Using setIcon() does absolutely nothing though, apparently; the items still show up as they used to, without icons.
I'm aware that this is probably not the way this function needs to be used, but I have found little documentation about it online, and absolutely no useful examples of implementations. My question is, can anybody help me with fixing this problem?
This is how you may conduct your debugging:
Try the constructor that has both icon and text as arguments.
Try to use that icon in another context to ensure it is displayable (construct a QIcon with same argument and use it elsewhere, e.g. QLabel!).
Use icon() from the QListWidgetItem to receive back the icon and then look at that QIcon.
Create a new QListWidget, change nothing, and ordinarily add some stock items in your MainWidget's constructor. See if the icons show up there.