• 6760阅读
  • 7回复

【提问】程序错在那里,怎么改正? [复制链接]

上一主题 下一主题
离线gongyh
 

只看楼主 正序阅读 楼主  发表于: 2005-09-09
我想用designer生成界面,现在想点击按钮“ok” 就执行dois函数,现在程序有问题,
谁能帮改一下。

#ifndef DISPLAYER_H
#define DISPLAYER_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtGui/QSplitter>
#include <QtGui/QTextEdit>
#include <QtGui/QWidget>

class Ui_MainWindow
{
public:
  QWidget *centralWidget;
  QWidget *widget;
  QWidget *widget1;
  QWidget *widget2;
  QWidget *widget3;
  QSplitter *splitter;
  QWidget *widget_2;
  QWidget *widget4;
  QSplitter *splitter1;
  QSplitter *splitter2;
  QSplitter *splitter_3;
  QLabel *label_2_2_2;
  QSplitter *splitter_2;
  QLabel *label_3_2_2_2_2_2;
  QLabel *label_4_2_2_2_2_2;
  QSplitter *splitter3;
  QLabel *label_5_2_2;
  QLineEdit *lineEdit_4;
  QLineEdit *lineEdit_3;
  QPushButton *pushButton_2;
  QPushButton *pushButton;
  QLineEdit *lineEdit;
  QLineEdit *lineEdit_2;
  QTextEdit *textEdit;
  QLabel *label;

  void setupUi(QMainWindow *MainWindow)
  {
  MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
  MainWindow->resize(QSize(185, 239).expandedTo(MainWindow->minimumSizeHint()));
  centralWidget = new QWidget(MainWindow);
  centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
  widget = new QWidget(centralWidget);
  widget->setObjectName(QString::fromUtf8("widget"));
  widget->setGeometry(QRect(80, 10, 90, 221));
  widget1 = new QWidget(centralWidget);
  widget1->setObjectName(QString::fromUtf8("widget1"));
  widget1->setGeometry(QRect(81, 11, 90, 191));
  widget2 = new QWidget(centralWidget);
  widget2->setObjectName(QString::fromUtf8("widget2"));
  widget2->setGeometry(QRect(82, 12, 90, 136));
  widget3 = new QWidget(centralWidget);
  widget3->setObjectName(QString::fromUtf8("widget3"));
  widget3->setGeometry(QRect(9, 13, 61, 175));
  splitter = new QSplitter(centralWidget);
  splitter->setObjectName(QString::fromUtf8("splitter"));
  splitter->setGeometry(QRect(10, 14, 133, 21));
  splitter->setOrientation(Qt::Horizontal);
  widget_2 = new QWidget(centralWidget);
  widget_2->setObjectName(QString::fromUtf8("widget_2"));
  widget_2->setGeometry(QRect(87, 294, 31, 16));
  widget4 = new QWidget(centralWidget);
  widget4->setObjectName(QString::fromUtf8("widget4"));
  widget4->setGeometry(QRect(10, 60, 88, 46));
  splitter1 = new QSplitter(centralWidget);
  splitter1->setObjectName(QString::fromUtf8("splitter1"));
  splitter1->setGeometry(QRect(10, 100, 120, 21));
  splitter1->setOrientation(Qt::Horizontal);
  splitter2 = new QSplitter(centralWidget);
  splitter2->setObjectName(QString::fromUtf8("splitter2"));
  splitter2->setGeometry(QRect(10, 140, 137, 21));
  splitter2->setOrientation(Qt::Horizontal);
  splitter_3 = new QSplitter(centralWidget);
  splitter_3->setObjectName(QString::fromUtf8("splitter_3"));
  splitter_3->setGeometry(QRect(10, 50, 111, 21));
  splitter_3->setOrientation(Qt::Horizontal);
  label_2_2_2 = new QLabel(centralWidget);
  label_2_2_2->setObjectName(QString::fromUtf8("label_2_2_2"));
  label_2_2_2->setGeometry(QRect(10, 40, 27, 21));
  splitter_2 = new QSplitter(centralWidget);
  splitter_2->setObjectName(QString::fromUtf8("splitter_2"));
  splitter_2->setGeometry(QRect(10, 90, 120, 21));
  splitter_2->setOrientation(Qt::Horizontal);
  label_3_2_2_2_2_2 = new QLabel(centralWidget);
  label_3_2_2_2_2_2->setObjectName(QString::fromUtf8("label_3_2_2_2_2_2"));
  label_3_2_2_2_2_2->setGeometry(QRect(10, 70, 29, 21));
  label_4_2_2_2_2_2 = new QLabel(centralWidget);
  label_4_2_2_2_2_2->setObjectName(QString::fromUtf8("label_4_2_2_2_2_2"));
  label_4_2_2_2_2_2->setGeometry(QRect(10, 100, 46, 21));
  splitter3 = new QSplitter(centralWidget);
  splitter3->setObjectName(QString::fromUtf8("splitter3"));
  splitter3->setGeometry(QRect(11, 132, 91, 17));
  splitter3->setOrientation(Qt::Horizontal);
  label_5_2_2 = new QLabel(splitter3);
  label_5_2_2->setObjectName(QString::fromUtf8("label_5_2_2"));
  lineEdit_4 = new QLineEdit(centralWidget);
  lineEdit_4->setObjectName(QString::fromUtf8("lineEdit_4"));
  lineEdit_4->setGeometry(QRect(67, 100, 111, 21));
  lineEdit_3 = new QLineEdit(centralWidget);
  lineEdit_3->setObjectName(QString::fromUtf8("lineEdit_3"));
  lineEdit_3->setGeometry(QRect(67, 70, 111, 21));
  pushButton_2 = new QPushButton(centralWidget);
  pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
  pushButton_2->setGeometry(QRect(100, 210, 75, 23));
  pushButton = new QPushButton(centralWidget);
  pushButton->setObjectName(QString::fromUtf8("pushButton"));
  pushButton->setGeometry(QRect(10, 210, 75, 23));
  lineEdit = new QLineEdit(centralWidget);
  lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
  lineEdit->setGeometry(QRect(67, 14, 111, 21));
  lineEdit_2 = new QLineEdit(centralWidget);
  lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
  lineEdit_2->setGeometry(QRect(67, 40, 111, 21));
  textEdit = new QTextEdit(centralWidget);
  textEdit->setObjectName(QString::fromUtf8("textEdit"));
  textEdit->setGeometry(QRect(10, 154, 168, 53));
  label = new QLabel(centralWidget);
  label->setObjectName(QString::fromUtf8("label"));
  label->setGeometry(QRect(10, 14, 52, 21));
  label->setTextFormat(Qt::AutoText);
  MainWindow->setCentralWidget(centralWidget);
  retranslateUi(MainWindow);

  QMetaObject::connectSlotsByName(MainWindow);
  } // setupUi

  void retranslateUi(QMainWindow *MainWindow)
  {
  MainWindow->setWindowTitle(QApplication::translate("MainWindow", "E_BOOK"));
  label_2_2_2->setText(QApplication::translate("MainWindow", "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\" white-space: pre-wrap; font-family:MS Shell Dlg; font-weight:400; font-style:normal; text-decoration:none;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Sex</span></p></body></html>"));
  label_3_2_2_2_2_2->setText(QApplication::translate("MainWindow", "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\" white-space: pre-wrap; font-family:MS Shell Dlg; font-weight:400; font-style:normal; text-decoration:none;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; color:#000000;\">Age</span></p></body></html>"));
  label_4_2_2_2_2_2->setText(QApplication::translate("MainWindow", "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\" white-space: pre-wrap; font-family:MS Shell Dlg; font-weight:400; font-style:normal; text-decoration:none;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; color:#000000;\">Phone</span></p></body></html>"));
  label_5_2_2->setText(QApplication::translate("MainWindow", "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\" white-space: pre-wrap; font-family:MS Shell Dlg; font-weight:400; font-style:normal; text-decoration:none;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; color:#000000;\">Address</span></p></body></html>"));
  pushButton_2->setText(QApplication::translate("MainWindow", "CANCE"));
  pushButton->setText(QApplication::translate("MainWindow", "OK"));
  label->setText(QApplication::translate("MainWindow", "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\" white-space: pre-wrap; font-family:MS Shell Dlg; font-weight:400; font-style:normal; text-decoration:none;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Name</span></p></body></html>"));
  Q_UNUSED(MainWindow);
  } // retranslateUi

};

namespace Ui {
  class MainWindow: public Ui_MainWindow {
      Q_OBJECT
  public:
      MainWindow();
  public slots:
      void dois();
      };
} // namespace Ui

#endif // DISPLAYER_H

/////////////////////////////////////////////////////////////////////////
#include "displayer.h"
#include <QFile>

using namespace Ui;
MainWindow::MainWindow(){
        connect(pushButton,SIGNAL(clicked()),this,SLOT(dois()));
                    }
MainWindow::dois()
{           char line[] = " hello world ,this is a Qt";
            QFile qfile("gyh.txt");
            if(qfile.open(IO_Append))
            {
            for(int i= 0;i<strlen(line1);i++)
                                  qfile.putch(line1[i]);
                            }
            qfile.close();
            }
}

///////////////////////////////////////////////////////////////////////////

#include <QApplication>

#include "displayer.h"

using namespace Ui;
int main(int argc, char *argv[])
{  
  QApplication app(argc,argv);
 
  QMainWindow mainwindow;

  MainWindow ui;
  ui.setupUi(&mainwindow);
  mainwindow.show();
  return app.exec();

}
[ 此贴被XChinux在2005-09-09 16:00重新编辑 ]
打工不是一辈子的事!
离线wuxian610
只看该作者 7楼 发表于: 2005-10-15
谢谢了先~!
请问QT支持哪些图片格式???
      代码如下:QT4.0.1. WIN2K
     
      QPixmap pixmap;
      QLabel label;

      pixmap.load(imgPath);
      label.setPixmap(pixmap);

    结果发现只有当图片格式是.jpg, .bmp 时才能使, 但我现在必须要把.wmf格式的图片显

示出来,请各位高手支招.
离线wuxian610
只看该作者 6楼 发表于: 2005-10-15
connect(ui.pushbutton, SIGNAL(clicked()), this, SLOT(confirm())); //写在面板构造函数里
//
void MainWindow::dois()
{       char line[] = " hello world ,this is a Qt";
        QFile qfile("gyh.txt");
        if(qfile.open(IO_Append))
        {
        for(int i= 0;i<strlen(line1);i++)
                      qfile.putch(line1);
                  }
        qfile.close();
        }
}

然后把void dois()声明写入MainWindow类private slots里.试试看.
                                                                  wuxian610
离线gongyh

只看该作者 5楼 发表于: 2005-09-09
那我怎么添加我自己想要的singal 和slots呢,是不是要写一个ui类的继承类吗?还是别的呀
打工不是一辈子的事!
离线XChinux

只看该作者 4楼 发表于: 2005-09-09
问题是你的那个类不是QMainWindow继承过来的
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线gongyh

只看该作者 3楼 发表于: 2005-09-09
我上面是三个文件。两个.cpp 一个.h文件
打工不是一辈子的事!
离线XChinux

只看该作者 2楼 发表于: 2005-09-09
一个界面专门写一个类放在那里使用,不要和main.cpp混在一起写,除非你不使用Q_OBJECT宏.
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线XChinux

只看该作者 1楼 发表于: 2005-09-09
为什么要采用这种方式,采用推荐的方式麻烦就少多了.
不推荐直接修改ui文件并且修改得不伦不类的在里面加东西.
这样出了问题别人给你找问题在哪里也无从下手,没有个良好的结构就.

这样moc编译器如何去生成moc_xxx源文件.
建议楼主详细看看置顶的两个初步学习QT的帖子.采用推荐的方式做.
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
快速回复
限100 字节
 
上一个 下一个