QLineEdit加入到QTableWidget的问题

louis_liux 2014-11-09 10:48:26

这个事QLineEdit 加入到TableWidget当中,最后一列,我想当点击QLineEdit就启动QT的调色板程序
然后选择颜色,确定之后把选择的颜色在TableWidget 中的QLineEdit中显示,这个该如何实现呢?
...全文
1435 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
晋晔 2016-11-21
  • 打赏
  • 举报
回复
mark~~~~~
michael2988 2014-11-11
  • 打赏
  • 举报
回复
引用 15 楼 louis_liux 的回复:
多亏问了下,那这个重写完之后,用法和Qlineedit用法一样吗,还这样用吗 LineEdit *letedit = new QLineEdit(""); ui->twEditMarkers->setCellWidget(i,2,letedit);
应该 new 新的类了。

LineEdit *letedit = new LineEdit("");
ui->twEditMarkers->setCellWidget(i,2,letedit);
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
多亏问了下,那这个重写完之后,用法和Qlineedit用法一样吗,还这样用吗 LineEdit *letedit = new QLineEdit(""); ui->twEditMarkers->setCellWidget(i,2,letedit);
michael2988 2014-11-11
  • 打赏
  • 举报
回复
引用 12 楼 louis_liux 的回复:
QLineEdit *letedit = new QLineEdit(""); ui->twEditMarkers->setCellWidget(i,2,letedit); 这是我的绑定到tablewidget的代码,看看这样对吗? void AddMarker01Dialog::mousePressEvent(QMouseEvent *event) { if (hasFocus()) { // show the dialog like before QMessageBox::information(this,tr("Information"),"QString::number(row)"); } }这是是这个响应的函数 这样写有问题吗 还需要信号和曹绑定吗
晕,写错了。 你要做的是从QLineEdit继承一个LineEdit,在新类里面加我之前的重写代码,你不是在TableWidget里面。

#include <QLineEdit>

class LineEdit : public QLineEdit
{
    Q_OBJECT

public:
    LineEdit(QWidget *parent = 0);
    ~LineEdit();

protected:
    //void focusInEvent(QFocusEvent *event);
    void mousePressEvent(QMouseEvent *event);
};
接下来新类LineEdit,可以像QLineEdit一样使用了。
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
引用 11 楼 michael2988 的回复:
[quote=引用 10 楼 louis_liux 的回复:] [quote=引用 9 楼 michael2988 的回复:] 把focusInEvent删除掉,只实现mousePressEvent,而且不用hasFocus判断,因为刚开始lineedit并没有焦点存在。
我都删除了啊,克就是了不响应呢,不知道我哪里写错了,是不是[/quote] hasFocus 也删掉了吗?不应该进不去的啊,你设个断点调试下。[/quote] 是的啊,我就打断点跟的呢啊,这个需要绑定信号和槽函数吗
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
QLineEdit *letedit = new QLineEdit(""); ui->twEditMarkers->setCellWidget(i,2,letedit); 这是我的绑定到tablewidget的代码,看看这样对吗? void AddMarker01Dialog::mousePressEvent(QMouseEvent *event) { if (hasFocus()) { // show the dialog like before QMessageBox::information(this,tr("Information"),"QString::number(row)"); } }这是是这个响应的函数 这样写有问题吗 还需要信号和曹绑定吗
michael2988 2014-11-11
  • 打赏
  • 举报
回复
引用 10 楼 louis_liux 的回复:
[quote=引用 9 楼 michael2988 的回复:] 把focusInEvent删除掉,只实现mousePressEvent,而且不用hasFocus判断,因为刚开始lineedit并没有焦点存在。
我都删除了啊,克就是了不响应呢,不知道我哪里写错了,是不是[/quote] hasFocus 也删掉了吗?不应该进不去的啊,你设个断点调试下。
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
引用 9 楼 michael2988 的回复:
把focusInEvent删除掉,只实现mousePressEvent,而且不用hasFocus判断,因为刚开始lineedit并没有焦点存在。
我都删除了啊,克就是了不响应呢,不知道我哪里写错了,是不是
michael2988 2014-11-11
  • 打赏
  • 举报
回复
把focusInEvent删除掉,只实现mousePressEvent,而且不用hasFocus判断,因为刚开始lineedit并没有焦点存在。
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
void AddMarker01Dialog::mousePressEvent(QMouseEvent *event) { if (hasFocus()) { // show the dialog like before QMessageBox::information(this,tr("Information"),"QString::number(row)"); } } 可是啊,我写的这个方法,它程序不走呢,当点击lineedit的时候,我写的有问题吗?
michael2988 2014-11-11
  • 打赏
  • 举报
回复
逻辑有点乱了,这样吧,如果你只用点击来产生 dialog,只重写mousePressEvent 就可以了。 focusInEvent界面切换也会响应,导致多次响应。

void LineEdit::mousePressEvent( QMouseEvent *event )
{
    m_beenShown = false;
    QColor initialColor(255, 0, 0);
    QColor pickColor = QColorDialog::getColor(initialColor, this, tr("Pick Color"));
    if (pickColor.isValid())
    {
        QString strColor = QString("(%1, %2, %3)").arg(pickColor.red()).arg(pickColor.green()).arg(pickColor.blue());
        setText(strColor);
    }
}
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
void AddMarker01Dialog::mousePressEvent(QMouseEvent *event) { if (hasFocus()) { // show the dialog like before QMessageBox::information(this,tr("Information"),"QString::number(row)"); } } 这样写的,对吗? 这个方法点击那个lineedit 这个方法都进不来呢
michael2988 2014-11-11
  • 打赏
  • 举报
回复
呵呵,if 写错了,应为

if (hasFocus())
{
    
}
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
引用 3 楼 michael2988 的回复:
[quote=引用 2 楼 louis_liux 的回复:] [quote=引用 1 楼 michael2988 的回复:] 重载 QLineEdit::focusInEvent()。

void LineEdit::focusInEvent( QFocusEvent *event )
{
    if (Qt::MouseFocusReason == event->reason()
        || Qt::TabFocusReason == event->reason())
    {
        QColor initialColor(255, 0, 0);
        QColor pickColor = QColorDialog::getColor(initialColor, this, tr("Pick Color"));
        if (pickColor.isValid())
        {
            QString strColor = QString("(%1, %2, %3)").arg(pickColor.red()).arg(pickColor.green()).arg(pickColor.blue());
            setText(strColor);
        }
    }
}
还需要设置哪里吗,我这刚进入到上面的画面能弹出来颜色对话框,之后就点击tablewidget里面的lineedit就弹不出来了 怎么回事呢?[/quote] 由于再次点击的时候,焦点没有发生变化,所以不会弹出。 你可以重载其mousePressEvent,但是要判断其是否有焦点

void LineEdit::mousePressEvent( QMouseEvent * event )
{
    if (!hasFocus())
    {
        // show the dialog like before
    }
}
[/quote] 再次点击的时候 focusInEvent( mousePressEvent 这个方法都不走是咋回事呢?
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
引用 29 楼 michael2988 的回复:
[quote=引用 28 楼 louis_liux 的回复:] [quote=引用 24 楼 michael2988 的回复:] 相应的控件类的API在Assistant都有的,你可以查看下的。
QPalette lette; QColor color(0,255,255); lette.setColor(QPalette::WindowText, color); lineedit->setPalette(lette); 和 lineedit->setStyleSheet("background-color:rgb(0,255,255)"); 达不到想要的效果该咋办呢[/quote] 如果你想设置文件颜色的话应该像下面:

lette.setColor(QPalette::Text, color);

lineedit->setStyleSheet("color:rgb(0,255,255)");
[/quote] 全都搞定,多谢大哥的热心帮助,没大哥这个问题估计得搞个2,3天
michael2988 2014-11-11
  • 打赏
  • 举报
回复
引用 28 楼 louis_liux 的回复:
[quote=引用 24 楼 michael2988 的回复:] 相应的控件类的API在Assistant都有的,你可以查看下的。
QPalette lette; QColor color(0,255,255); lette.setColor(QPalette::WindowText, color); lineedit->setPalette(lette); 和 lineedit->setStyleSheet("background-color:rgb(0,255,255)"); 达不到想要的效果该咋办呢[/quote] 如果你想设置文件颜色的话应该像下面:

lette.setColor(QPalette::Text, color);

lineedit->setStyleSheet("color:rgb(0,255,255)");
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
引用 24 楼 michael2988 的回复:
相应的控件类的API在Assistant都有的,你可以查看下的。
QPalette lette; QColor color(0,255,255); lette.setColor(QPalette::WindowText, color); lineedit->setPalette(lette); 和 lineedit->setStyleSheet("background-color:rgb(0,255,255)"); 达不到想要的效果该咋办呢
michael2988 2014-11-11
  • 打赏
  • 举报
回复
由于你要读取,所以在保存的时候可以改变下:

ts << color.name() << "\n";
这样读取就比较方便
michael2988 2014-11-11
  • 打赏
  • 举报
回复

    QString binPath = QCoreApplication::applicationDirPath();
    QString filePath = binPath + "/color.txt";
    QFile file(filePath);
    if (!file.open(QFile::ReadOnly | QIODevice::Text))
    {
        return;
    }
    QTextStream ts(&file);
    QStringList slColor;
    while (!ts.atEnd())
    {
        slColor << ts.readLine();
    }
    file.close();
    
    QPalette palette;
    for (int i = 0; i < m_table->rowCount() && i < slColor.size(); i++)
    {
        LineEdit* le = (LineEdit *)m_table->cellWidget(i, 2);
        QColor color(slColor.at(i));
        palette.setColor(QPalette::Base, color);
        le->setPalette(palette);
    }
louis_liux 2014-11-11
  • 打赏
  • 举报
回复
引用 23 楼 michael2988 的回复:

    QString binPath = QCoreApplication::applicationDirPath();
    QString filePath = binPath + "/color.txt";
    QFile file(filePath);
    if (!file.open(QFile::WriteOnly | QIODevice::Text))
    {
        return;
    }
    QTextStream ts(&file);
    for (int i = 0; i < m_table->rowCount(); i++)
    {
        LineEdit* le = (LineEdit *)m_table->cellWidget(i, 2);
        QColor color = le->palette().color(QPalette::Base);
        ts << QString("(%1, %2, %3)").arg(color.red()).arg(color.green()).arg(color.blue());
    }
    ts.flush();
    file.close();
新建color.txt于你exe同一个目录下。
这个怎么把从文本文件获取的颜色值,加入到放入到tablewidget中的lineedit,中显示颜色呢
加载更多回复(11)
第7章 Qt基于Widget的控件 137 7.1 QLabel控件 137 设置QLabel字体的大小和颜色 137 使用QLabel显示图片 138 图片自适应QLabel的大小 138 7.2 QPushButton控件 138 QPushButton的基本应用 139 QPushButton设置图片 140 7.3 QLineEdit控件 141 QLineEdit基本应用 141 QLineEdit限制输入数字 141 setPlaceholderText()设置提示文字 142 setReadOnly设置不可编辑 142 setMaxLength()设置可以输入的最多字符数 142 setEchoMode()设置模式 142 7.4 QTextEdit控件 143 简介 143 提示占位文本 143 文本内容设置 144 7.4 QPlainTextEdit控件 145 QPlainTextEdit简介 145 QPlainTextEdit富文本 145 QPlainTextEdit用法 145 7.5 QSpinBox控件 146 QSpinBox简介 146 QSpinBox的主要属性 147 QSpinBox案例分析 147 7.6 QRadioButton控件 148 简介 148 在UI界面中加入QRadioButton控件 148 对QRadioButton控件进行分组 149 多个QRadioButton控件响应同一个槽函数 149 7.7 QCheckBox控件 152 简介 152 QCheckBox开启三态模式 152 在UI界面中加入QCheckBox控件 153 stateChanged()信号 153 猜猜你喜欢 154 7.8 QSlider滑动条控件 155 QSlider简介 155 QSlider案例 156 7.9 QComboBox下拉选择框 157 QComboBox简介 157 QComboBox列表项的访问 157 QComboBox的currentlndexChanged信号 157 QComboBox添加图标 159 用代码添加项 159 添加项 159 7.10 列表框QListWidget类 160 QListWidget简介 160 列表项的显示设置 161 列表框常用操作 162 添加操作 162 删除操作 162 7.11 表格控件QTableWidget 163 简介 163 界面设计器中编辑 163 代码方式修改QTableWidget 164 7.12 树控件QTreeWidget 167 简介 167 操作步骤与分析 167 7.13 Qt的日期、时间、日历等控件 169 QDateEdit控件 169 QDateTimeEdit控件 169 QCalendarWidget控件 170 7.14 QDialog 171 简介 171 操作 171 QMessageBox 172 QFileDialog 173 QFontDialog 174 QColorDialog 175
中国象棋的C++代码 #include "chess_zn.h" QTcpSocket * Chess_ZN::client = new QTcpSocket; QUndoStack * Chess_ZN::undoStack = new QUndoStack(); int Chess_ZN::second = 120; bool Chess_ZN::isTurn = false; Chess_ZN::Chess_ZN(QWidget *parent) : QWidget(parent) { init(); initElse(); } void Chess_ZN::initElse(){ treeitem = 1; timer=new QTimer; portmap=0; isConn = true; start = false; isTimer = false; isSearch = false; connect(timer,SIGNAL(timeout()),this,SLOT(stopWatch())); connect(wigettree[1],SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(getInfo(QTreeWidgetItem*))); connect(wigettree[0],SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(connectToHost_PK(QTreeWidgetItem*))); connect(client,SIGNAL(connected()),this,SLOT(connected())); //连接一旦断开 connect(client,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error(QAbstractSocket::SocketError ))); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); peer = new PeerManager(this); peer->setServerPort(10001); items=wigettree[1]->currentItem(); item_pk=wigettree[0]->currentItem(); item_pk_info=wigettree[0]->currentItem(); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[8], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[9], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[10], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[11], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[0], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[1], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[2], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[3], SLOT(setEnabled(bool))); timer->start(1000); createUndoView(); isChoose = true; tableeditor=new TableEditor("users"); } void Chess_ZN::createUndoView() { undoVie

16,202

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧