Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Mousepressed and show labels
Qt 6.11 is out! See what's new in the release blog

Mousepressed and show labels

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 345 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    reis2211
    wrote on last edited by Chris Kawa
    #1

    Hey, I'm plotting GPS coordinates into a graph. However, I want to, when a point is pressed, a label, which is initially hidden, appears showing other information I need. How can I do it? I've been reading about mouse-pressed events, but I couldn't find a solution.
    Here is my code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFileDialog>
    #include <QFile>
    #include <QMessageBox>
    #include "dms.h"
    #include <QDebug>
    #include <QPen>
    #include <iostream>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
        QString linha,latitude,longitude,direcao1,direcao2,tempo;
        QStringList list,aux;
        QVector<double> lati,longi,time;
        QString lineNames;
        QPen pen;
        float constante;
        constante=0;
    
        lineNames="lsNone";
    
        QFile file(QFileDialog::getOpenFileName(this,tr("Open File"),"",tr("Text Files (*.txt)"))); // escolha do ficheiro no pc do utilizador
    
        if(!file.open(QIODevice::ReadOnly))                    //abertura do ficheiro em segurança no modo read
            QMessageBox::information(0,"info",file.errorString());
    
        QTextStream in(&file);
    
         while(!in.atEnd())
         {
             linha=in.readLine();
    
             list=linha.split(",");
             tempo=list[1];
             time.append(ajustatempos(tempo,&constante)),
             latitude=list[2];
             direcao1=list[3];
             lati.append(nmea_para_graus(latitude,direcao1));
             longitude=list[4];
             direcao2=list[5];
    
             longi.append(nmea_para_graus(longitude,direcao2));
    
         }
         QVector<double> x,y;
         QVector<double> z,r,w;
         int i;
         int volta=0;
         ui->widget->addGraph();
         for(i=3300;i<10000;i++)
         {
             x.append(lati[i]);
             y.append(longi[i]);
             z.append(abs(lati[i]-38.74910));
             r.append(abs(longi[i]+9.3925));
             w.append(z[i-3300]+r[i-3300]);
             // add the text label at the top:
             QCPItemText *textLabel = new QCPItemText(ui->widget);
             textLabel->setPositionAlignment(Qt::AlignTop|Qt::AlignHCenter);
             textLabel->position->setType(QCPItemPosition::ptAxisRectRatio);
             textLabel->position->setCoords(0.5,0.7); // place position
             textLabel->setText(QString::number(time[i]));
             textLabel->setFont(QFont(font().family(), 7)); // make font a bit larger
             textLabel->setPen(QPen(Qt::black));// show black border around text
    
             // add the arrow:
             QCPItemLine *arrow = new QCPItemLine(ui->widget);
             arrow->start->setParentAnchor(textLabel->bottom);
             arrow->end->setCoords(x[i-3300],y[i-3300]); // point  in x-y-plot coordinates
             arrow->setHead(QCPLineEnding::esSpikeArrow);
             textLabel->setVisible(false);
             arrow->setVisible(false);
    
             if(w[i-3300]<0.00055 && i>3302 && w[i-3301]>0.00055)
             {
                 break;
             }
    
         }
    
    
         ui->widget->graph(0)->setData(x,y);
         // give the axes some labels:
         ui->widget->xAxis->setLabel("x");
         ui->widget->yAxis->setLabel("y");
         pen.setColor(QColor(qSin(1.2)*80+80,80+80, qSin(1.5)*80+80));
         ui->widget->graph()->setPen(pen);
         ui->widget->graph()->setLineStyle((QCPGraph::LineStyle)QCPGraph::lsNone);
         ui->widget->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5));
    
    
         // set axes ranges, so we see all data:
         ui->widget->xAxis->setRange(36, 38);
         ui->widget->yAxis->setRange(-7, -9);
         ui->widget->setInteraction(QCP::iRangeZoom, true);
         ui->widget->replot();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Did you check whether the folks behind QCustomPlot already provides something for what you want to show ?

      If not, you can subclass the plot class you are using and reimplement the mouse related methods.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved