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. how to use class from internet for my project?
Qt 6.11 is out! See what's new in the release blog

how to use class from internet for my project?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 497 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.
  • M Offline
    M Offline
    mario3
    wrote on last edited by
    #1

    i am beginner in c++ and I want to know how to use this class which I found on the internet:

    scrolltext.h:

    #ifndef SCROLLTEXT_H
    #define SCROLLTEXT_H
    
    #include <QWidget>
    #include <QStaticText>
    #include <QTimer>
    
    
    class ScrollText : public QWidget
    {
        Q_OBJECT
        Q_PROPERTY(QString text READ text WRITE setText)
        Q_PROPERTY(QString separator READ separator WRITE setSeparator)
    
    public:
        explicit ScrollText(QWidget *parent = 0);
    
    public slots:
        QString text() const;
        void setText(QString text);
    
        QString separator() const;
        void setSeparator(QString separator);
    
    
    protected:
        virtual void paintEvent(QPaintEvent *);
        virtual void resizeEvent(QResizeEvent *);
    
    private:
        void updateText();
        QString _text;
        QString _separator;
        QStaticText staticText;
        int singleTextWidth;
        QSize wholeTextSize;
        int leftMargin;
        bool scrollEnabled;
        int scrollPos;
        QImage alphaChannel;
        QImage buffer;
        QTimer timer;
    
    private slots:
        virtual void timer_timeout();
    };
    
    #endif // SCROLLTEXT_H
    

    scrolltext.cpp:

    #include "scrolltext.h"
    #include <QPainter>
    
    
    ScrollText::ScrollText(QWidget *parent) :
        QWidget(parent), scrollPos(0)
    {
        staticText.setTextFormat(Qt::PlainText);
    
    
        setFixedHeight(fontMetrics().height());
        leftMargin = height() / 3;
    
        setSeparator("   ---   ");
    
        connect(&timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
        timer.setInterval(50);
    }
    
    QString ScrollText::text() const
    {
        return _text;
    }
    
    void ScrollText::setText(QString text)
    {
        _text = text;
        updateText();
        update();
    }
    
    QString ScrollText::separator() const
    {
        return _separator;
    }
    
    void ScrollText::setSeparator(QString separator)
    {
        _separator = separator;
        updateText();
        update();
    }
    
    void ScrollText::updateText()
    {
        timer.stop();
    
        singleTextWidth = fontMetrics().QFontMetrics::horizontalAdvance(_text);
        scrollEnabled = (singleTextWidth > width() - leftMargin);
    
        if(scrollEnabled)
        {
            scrollPos = -64;
            staticText.setText(_text + _separator);
            timer.start();
        }
        else
            staticText.setText(_text);
    
        staticText.prepare(QTransform(), font());
        wholeTextSize = QSize(fontMetrics().QFontMetrics::horizontalAdvance(staticText.text()), fontMetrics().height());
    }
    
    void ScrollText::paintEvent(QPaintEvent*)
    {
        QPainter p(this);
    
        if(scrollEnabled)
        {
            buffer.fill(qRgba(0, 0, 0, 0));
            QPainter pb(&buffer);
            pb.setPen(p.pen());
            pb.setFont(p.font());
    
            int x = qMin(-scrollPos, 0) + leftMargin;
            while(x < width())
            {
                pb.drawStaticText(QPointF(x, (height() - wholeTextSize.height()) / 2) + QPoint(2, 2), staticText);
                x += wholeTextSize.width();
            }
    
            //Apply Alpha Channel
            pb.setCompositionMode(QPainter::CompositionMode_DestinationIn);
            pb.setClipRect(width() - 15, 0, 15, height());
            pb.drawImage(0, 0, alphaChannel);
            pb.setClipRect(0, 0, 15, height());
            //initial situation: don't apply alpha channel in the left half of the image at all; apply it more and more until scrollPos gets positive
            if(scrollPos < 0)
                pb.setOpacity((qreal)(qMax(-8, scrollPos) + 8) / 8.0);
            pb.drawImage(0, 0, alphaChannel);
    
            //pb.end();
            p.drawImage(0, 0, buffer);
        }
        else
        {
            p.drawStaticText(QPointF(leftMargin, (height() - wholeTextSize.height()) / 2), staticText);
        }
    }
    
    void ScrollText::resizeEvent(QResizeEvent*)
    {
        //When the widget is resized, we need to update the alpha channel.
    
        alphaChannel = QImage(size(), QImage::Format_ARGB32_Premultiplied);
        buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
    
        //Create Alpha Channel:
        if(width() > 64)
        {
            //create first scanline
            QRgb* scanline1 = (QRgb*)alphaChannel.scanLine(0);
            for(int x = 1; x < 16; ++x)
                scanline1[x - 1] = scanline1[width() - x] = qRgba(0, 0, 0, x << 4);
            for(int x = 15; x < width() - 15; ++x)
                scanline1[x] = qRgb(0, 0, 0);
            //copy scanline to the other ones
            for(int y = 1; y < height(); ++y)
                memcpy(alphaChannel.scanLine(y), (uchar*)scanline1, width() * 4);
        }
        else
            alphaChannel.fill(qRgb(0, 0, 0));
    
    
        //Update scrolling state
        bool newScrollEnabled = (singleTextWidth > width() - leftMargin);
        if(newScrollEnabled != scrollEnabled)
            updateText();
    }
    
    void ScrollText::timer_timeout()
    {
        scrollPos = (scrollPos + 2)
                    % wholeTextSize.width();
        update();
    }
    

    and i have mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "scrolltext.h"
    #include <QWidget>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        timer = new QTimer(this);
    
        connect(timer,SIGNAL(timeout()),this,SLOT(myfunction()));
        timer ->start(1000);
    
    
    
    
    
    }
    
    MainWindow::~MainWindow()
    {
    
        delete ui;
    }
    
    
    
    void MainWindow::myfunction()
    
    {
    
        ui->animation->setText("this text is animated...this text is animated...");
    
    }
    

    and I want to use ScrollText() class to animation in myfunction() which is in mainwindow.cpp how can i do that?
    i try to call ScrollText().animation; but that not working

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to teh forums.
      It seems to have its own timer and all.
      So not sure you need timer in MainWindow
      You dont need any extra code besides the scroller.

      Did you use the promotion to add it to the UI ?

      I looked at code. it seems it only scroll if its too small for all text ?
      so make sure your widget is not HUGE.

       scrollEnabled = (singleTextWidth > width() - leftMargin);
      
          if(scrollEnabled)
          {
              scrollPos = -64;
              staticText.setText(_text + _separator);
              timer.start();  // only place it does start timer !
          }
      

      I just placed one on the form. (using promote) and then called
      ui->scroller->setText("HELLO TO YOU very long freaking long text");

      alt text

      M 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi and welcome to teh forums.
        It seems to have its own timer and all.
        So not sure you need timer in MainWindow
        You dont need any extra code besides the scroller.

        Did you use the promotion to add it to the UI ?

        I looked at code. it seems it only scroll if its too small for all text ?
        so make sure your widget is not HUGE.

         scrollEnabled = (singleTextWidth > width() - leftMargin);
        
            if(scrollEnabled)
            {
                scrollPos = -64;
                staticText.setText(_text + _separator);
                timer.start();  // only place it does start timer !
            }
        

        I just placed one on the form. (using promote) and then called
        ui->scroller->setText("HELLO TO YOU very long freaking long text");

        alt text

        M Offline
        M Offline
        mario3
        wrote on last edited by
        #3

        @mrjj yes its scroll only when text is bigger than label,
        yes!!! its work for me now, i set wrong name to the promotion...thx! :)

        mrjjM Pablo J. RoginaP 2 Replies Last reply
        1
        • M mario3

          @mrjj yes its scroll only when text is bigger than label,
          yes!!! its work for me now, i set wrong name to the promotion...thx! :)

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mario3
          Ok super. its a nice scroller :)

          1 Reply Last reply
          0
          • M mario3

            @mrjj yes its scroll only when text is bigger than label,
            yes!!! its work for me now, i set wrong name to the promotion...thx! :)

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @mario3 said in how to use class from internet for my project?:

            its work for me now,

            great, so please don't forget to mark your post as solved!

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1

            • Login

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