• 4197阅读
  • 9回复

[提问]程序无法在状态栏显示文本光标的行和列坐标? [复制链接]

上一主题 下一主题
离线luoqice
 

只看楼主 倒序阅读 楼主  发表于: 2013-03-19
我的程序为什么无法在状态栏显示光标的行和列呢?检查好久不知为啥。以下是我部分程序,我是初学者,请各位大侠多指教。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtGui>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    isSaved=false;
    curFile=tr("未命名.txt");
    setWindowTitle(curFile);
    init_statusBar();
    connect(ui->textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(do_cursorChange()));
}


void MainWindow::init_statusBar()


{


QStatusBar*bar=ui->statusBar;//获取状态栏
first_statusLabel=new QLabel;
first_statusLabel->setMinimumSize(150,20);
first_statusLabel->setFrameShape(QFrame::WinPanel);
first_statusLabel->setFrameShadow(QFrame::Sunken);


second_statusLabel=new QLabel;
second_statusLabel->setMinimumSize(150,20);
second_statusLabel->setFrameShape(QFrame::WinPanel);
second_statusLabel->setFrameShadow(QFrame::Sunken);


bar->addWidget(first_statusLabel);
bar->addWidget(second_statusLabel);


first_statusLabel->setText(tr("欢迎使用文本编辑框"));//显示编辑框
second_statusLabel->setText(tr("luoqice make!"));


}


void MainWindow::do_cursorChange()
{
int rowNum=ui->textEdit->document()->blockCount();
//获取光标所在的行号
const QTextCursor cursor=ui->textEdit->textCursor();
int colNum=cursor.columnNumber();
//获取光标所在的列号
first_statusLabel->setText(tr("%1 行 %2 列").arg(rowNum).arg(colNum));
//在状态栏显示光标


}
离线jxhxfen2008

只看该作者 1楼 发表于: 2013-03-19
Re:QT4学习问题


你试着在textedit里边按下enter键,到下一行,看看情况
离线luoqice

只看该作者 2楼 发表于: 2013-03-19
还是不行啊,connect(ui->textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(do_cursorChange()));好像是这个关联函数没有进入循环,不知放在哪个位置才行。
离线jxhxfen2008

只看该作者 3楼 发表于: 2013-03-19
用qdebug调试,看看
离线luoqice

只看该作者 4楼 发表于: 2013-03-19
不会用啊,求指点。
离线jxhxfen2008

只看该作者 5楼 发表于: 2013-03-19
百度
离线realfan

只看该作者 6楼 发表于: 2013-03-20
我用你的代码试了一下,完全没有问题。
估计是你没有写slots

public slots:
    void do_cursorChange();
离线realfan

只看该作者 7楼 发表于: 2013-03-20
附件是我测试的工程
untitled1.zip (3 K) 下载次数:1
离线luoqice

只看该作者 8楼 发表于: 2013-03-20
回 7楼(realfan) 的帖子
那我试下,谢啦!
离线luoqice

只看该作者 9楼 发表于: 2013-03-20
回 7楼(realfan) 的帖子
可以了,原来是我的void do_cursorChange();写在了private:那里,谢谢7楼了!
快速回复
限100 字节
 
上一个 下一个