вызову paintevent самостоятельно в другом классе в qt c ++ - PullRequest
0 голосов
/ 07 марта 2019

Я новичок в qt и делаю визуализацию Red black tree, используя qt c ++. Я создал структуру узла с помощью Qpaint, и я готов к базовому древовидному коду структуры данных, единственная проблема - это обновление события рисования, потому что я хочу вызывать событие рисования, когда захочу.

, а также при рисовании мои начальные узлы исчезают. пройдите код и помогите.

код:

//dispaymenu.cpp

#include "displaymenu.h"
#include "ui_displaymenu.h"
#include "datastruct.cpp"
#include<QtCore>
#include<QtGui>

DisplayMenu::DisplayMenu(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DisplayMenu)
{
    ui->setupUi(this);

}
DisplayMenu::~DisplayMenu()
{
    delete ui;
}
void DisplayMenu::paintEvent(QPaintEvent *)
{
        createNode(text,col);
}

void DisplayMenu::createNode(QString text,char col)
{

    QRectF rect(x,y,80,80);    //create rectangle object which is not seen
    QPainter p(this);
    if(col=='b')
        p.setBrush(Qt::black);
    else if(col=='r')
        p.setBrush(Qt::red);
    p.drawEllipse(rect);        //circle fits into the rect passed
    p.setPen(Qt::white);
    p.setFont(QFont("Arial", 15));      //to set font and size of text
    p.drawText(rect, Qt::AlignCenter,text);
}

void DisplayMenu::setText()
{
    bool ok;
    text = QInputDialog::getText(this, tr("getText()"),
                                         tr("getdata:"), QLineEdit::Normal,
                                         QDir::home().dirName(), &ok);
    if (ok && !text.isEmpty())
    {
        int ele=text.toInt();
        RBTree t1(a);
        col=t1.insert(ele);
        a=1;
        createNode(text,col);
    }
}


void DisplayMenu::on_textButton_clicked()
{
    setText();
}

this was our mainwindow file.

//main.cpp
#include "displaymenu.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    displaymenu w;
    w.show();

    return a.exec();
}


//datastruct.cpp

#include <bits/stdc++.h>
#include<datastruct.h>
#include<displaymenu.h>
#include"displaymenu.cpp"


using namespace std;

enum Color {RED, BLACK};

struct Node
{
    int data;
    char color;
    Node *left, *right, *parent;

    // Constructor
    Node(int data)
    {
        this->data = data;
        this->color='r';
        left = right = parent =nullptr;
    }
};

// Class to represent Red-Black Tree
class RBTree
{
public:
    Node *root;
    void rotateLeft(Node *&, Node *&);
    void rotateRight(Node *&, Node *&);
    void fixViolation(Node *&, Node *&);


    // Constructor
    RBTree(int a)
    {
        if(a==0)
            root = nullptr;
    }
    char insert(int ele);
    //void inorder();
    //void levelOrder();
};
Node* BSTInsert(Node* root, Node *pt)
{
    //DisplayMenu d(&p);
    /* If the tree is empty, return a new node */
    if (root == nullptr)
    {
        pt->color='b';
        return pt;
    }


    /* Otherwise, recur down the tree */
    if (pt->data < root->data)
    {
        root->left = BSTInsert(root->left, pt);
        root->left->parent = root;
    }
    else if (pt->data > root->data)
    {
        root->right = BSTInsert(root->right, pt);
        root->right->parent = root;
    }

    /* return the (unchanged) node pointer */
    return root;
}
char RBTree::insert(int data)
{
    Node *pt = new Node(data);

    // Do a normal BST insert
    root=BSTInsert(root, pt);
    return pt->color;

    // fix Red Black Tree violations
    //fixViolation(root, pt);
}

1 Ответ

1 голос
/ 07 марта 2019

Позвоните QWidget::update(), чтобы создать новое событие рисования на вашем виджете.Событие будет обработано в цикле событий как можно скорее.

Вы также можете принудительно перерисовать вызов QWidget::repaint, но вы остановите цикл событий (так, только если вам нужномгновенный перекрас).

...